Forum: Ruby on Rails "Selecting" a calculated row / Using :select in the paginate

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Iván Vega R. (Guest)
on 2006-01-06 03:25
(Received via mailing list)

I was trying to use the :select parameter to select a calculated row,
but it appears that the parameter is not picked up by the paginate

I want to add the following to the select statement:

timediff(now(), created_on) as age

So I tried:

@story_pages, @stories = paginate :stories, {:per_page => 10, :include
=> 'user', :select => 'timediff(now(), story.created_on) as story_age'}

... to no avail.

I know I can create a method named "age" for the story model that can
retrieve that calculation, but that's a query for each element, so it's
very undesirable.

In case you're wondering why I don't calculate the "age" inside my Rails
app, check this blog:

If you know of a solution or alternative, I'll greatly appreciate your

Ivan V.
Kevin O. (Guest)
on 2006-01-06 04:03
Iván Vega R. wrote:
> @story_pages, @stories = paginate :stories, {:per_page => 10, :include
> => 'user', :select => 'timediff(now(), story.created_on) as story_age'}
> ... to no avail.

I'm somewhat sure that paginate (and find for that matter) don't know
what to do with the :select option.

Try using find_by_sql()
Wilson B. (Guest)
on 2006-01-06 16:29
(Received via mailing list)
Check out this code snippet:
Even better is the updated version in 'toolmantim's' post at the bottom.
Iván Vega R. (Guest)
on 2006-01-06 22:00
(Received via mailing list)
Iván Vega R. (Guest)
on 2006-01-06 22:03
(Received via mailing list)
This topic is locked and can not be replied to.