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

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
C5380b957ff8ce5cb14cf2282ffeb720?d=identicon&s=25 Iván Vega Rivera (Guest)
on 2006-01-06 02:25
(Received via mailing list)
Hi,

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
method.

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:

http://railsexpress.de/blog/articles/2005/11/09/ac...

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

Sincerely,
Ivan V.
C8a634a01a2c4508360874bff7fb1a7f?d=identicon&s=25 Kevin Olbrich (Guest)
on 2006-01-06 03:03
Iván Vega Rivera 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()
25e11a00a89683f7e01e425a1a6e305c?d=identicon&s=25 Wilson Bilkovich (Guest)
on 2006-01-06 15:29
(Received via mailing list)
Check out this code snippet:
http://bigbold.com/snippets/posts/show/389
Even better is the updated version in 'toolmantim's' post at the bottom.
C5380b957ff8ce5cb14cf2282ffeb720?d=identicon&s=25 Iván Vega Rivera (Guest)
on 2006-01-06 21:00
(Received via mailing list)
Thanks!
C5380b957ff8ce5cb14cf2282ffeb720?d=identicon&s=25 Iván Vega Rivera (Guest)
on 2006-01-06 21:03
(Received via mailing list)
This topic is locked and can not be replied to.