Iván Vega Rivera
2006-Jan-06 01:24 UTC
[Rails] "Selecting" a calculated row / Using :select in the paginate method
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/accelerate-your-dates If you know of a solution or alternative, I''ll greatly appreciate your help. Sincerely, Ivan V.
Kevin Olbrich
2006-Jan-06 02:03 UTC
[Rails] Re: "Selecting" a calculated row / Using :select in the pagi
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() -- Posted via http://www.ruby-forum.com/.
Wilson Bilkovich
2006-Jan-06 14:29 UTC
[Rails] "Selecting" a calculated row / Using :select in the paginate method
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. On 1/5/06, Iv?n Vega Rivera <ivanvega@gmail.com> wrote:> 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/accelerate-your-dates > > If you know of a solution or alternative, I''ll greatly appreciate your help. > > Sincerely, > Ivan V. > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Iván Vega Rivera
2006-Jan-06 20:00 UTC
[Rails] "Selecting" a calculated row / Using :select in the paginate method
Thanks! Wilson Bilkovich wrote:> 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. > > On 1/5/06, Iv?n Vega Rivera <ivanvega@gmail.com> wrote: > >> 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/accelerate-your-dates >> >> If you know of a solution or alternative, I''ll greatly appreciate your help. >> >> Sincerely, >> Ivan V. >> >> _______________________________________________ >> Rails mailing list >> Rails@lists.rubyonrails.org >> http://lists.rubyonrails.org/mailman/listinfo/rails >> >> > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails > >
Iván Vega Rivera
2006-Jan-06 20:01 UTC
[Rails] Re: "Selecting" a calculated row / Using :select in the pagi
Check http://railsexpress.de/blog/articles/2005/11/06/the-case-for-piggy-backed-attributes ;-) Kevin Olbrich wrote:> 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() > >