Topic: Pluralize and a few others.

Hi, I'd please like to know how I can use the pluralize method in a rails controller class. It seems to only work in rhtml files.

In my view I can use: <%=  pluralize(post_comments.length,'comment')  %>
How can I use the pluralize method in my controller? Or perhaps, is there code available for the pluralize method I can copy and paste into my controller?

Also, how is the best way to delete an ActiveRecord collection. Say I find all comments which match a certain condition, what is the best way to delete this collection from the database cleanly?

Then last but maybe not least. I want to format dates retrieved from my database (I'm using sqlite). The dates are saved in the format "0000-00-00 00:00:00", How can I format this so that the date could be something like, "1st July 2006, 3:30AM".

Thanks

Re: Pluralize and a few others.

daibatzu wrote:

In my view I can use: <%=  pluralize(post_comments.length,'comment')  %>
How can I use the pluralize method in my controller? Or perhaps, is there code available for the pluralize method I can copy and paste into my controller?

Include the ActionView::Helpers::TextHelper into your controller. For example:

class BlogController < ApplicationController
  include ActionView::Helpers::TextHelper
  # ...
end

You will then have access to all of the text helper methods in your controller.

daibatzu wrote:

Also, how is the best way to delete an ActiveRecord collection. Say I find all comments which match a certain condition, what is the best way to delete this collection from the database cleanly?

Try the "delete_all" method and pass the condition of the items you want to delete.
http://api.rubyonrails.com/classes/Acti … ml#M000869

daibatzu wrote:

Then last but maybe not least. I want to format dates retrieved from my database (I'm using sqlite). The dates are saved in the format "0000-00-00 00:00:00", How can I format this so that the date could be something like, "1st July 2006, 3:30AM".

The "strftime" method can format a time.
http://www.ruby-doc.org/core/classes/Time.html#M000237

For example:

# This assumes there is a Post model with a "created_at" column which contains a time.
post.created_at.strftime('%B %d %Y, %I:%M %p')

I wouldn't be surprised if there was a more rails-ish way to do it, but I don't know how off the top of my head.

Railscasts - Free Ruby on Rails Screencasts

Re: Pluralize and a few others.

ryanb wrote:
daibatzu wrote:

Then last but maybe not least. I want to format dates retrieved from my database (I'm using sqlite). The dates are saved in the format "0000-00-00 00:00:00", How can I format this so that the date could be something like, "1st July 2006, 3:30AM".

The "strftime" method can format a time.
http://www.ruby-doc.org/core/classes/Time.html#M000237

For example:

# This assumes there is a Post model with a "created_at" column which contains a time.
post.created_at.strftime('%B %d %Y, %I:%M %p')

I wouldn't be surprised if there was a more rails-ish way to do it, but I don't know how off the top of my head.

That is the best way for now, you could write your own method, but it would belong to the redundant department of redundancy.

Last edited by Reedy (2006-07-01 21:09:31)

Most code examples are usually pulled out of the air and not tested. Use at your own risk!

Re: Pluralize and a few others.

Wow, you guys are hardcore. Thanks a lot. Just started ruby and rails a month ago but hope I will soon be good enough to answer questions here as well.
Thanks:-)