Adam
2011-Aug-28 17:52 UTC
Best form in pulling the last two records from a dataset in active record
I have a model, blog_posts which has a field "published_at". I''d like to select the latest two blogs from that model to display on my homepage. Not sure how to structure that though. At the moment I have a work around that takes a slice of the data but it keeps failing when I have nothing in the table, rather than fix this I feel there is probably a better way to retrieve the data. I need to select the blogs in separate calls, for example @blog_post.latestpost, @blog_post.secondlatestpost Thanks, Adam -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Jeroen van Ingen
2011-Aug-28 18:25 UTC
Re: Best form in pulling the last two records from a dataset in active record
Create a named_scope (scope in Rails 3) like this: scope :secondlatestpost, :limit => 2, :order => :published_at Then declare it as follows: BlogPost.secondlatestpost BTW IMHO the method name ''secondlatestpost'' is a little long. I should take a name like BlogPost.latest -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Hassan Schroeder
2011-Aug-28 19:04 UTC
Re: Re: Best form in pulling the last two records from a dataset in active record
On Sun, Aug 28, 2011 at 11:25 AM, Jeroen van Ingen <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Create a named_scope (scope in Rails 3) like this: > scope :secondlatestpost, :limit => 2, :order => :published_atOr for some flexibility: scope :latest, lambda {|number| { :order => :updated_at, :limit => number } }> BlogPost.latestthen you can ask for e.g. BlogPost.latest 2 or BlogPost.latest 5 (and with no number specified get them all in update order). FWIW, -- Hassan Schroeder ------------------------ hassan.schroeder-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org http://about.me/hassanschroeder twitter: @hassan -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Tom Meinlschmidt
2011-Aug-28 19:21 UTC
Re: Best form in pulling the last two records from a dataset in active record
On Aug 28, 2011, at 21:04 , Hassan Schroeder wrote:> On Sun, Aug 28, 2011 at 11:25 AM, Jeroen van Ingen <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: >> Create a named_scope (scope in Rails 3) like this: >> scope :secondlatestpost, :limit => 2, :order => :published_at > > Or for some flexibility: > > scope :latest, lambda {|number| { :order => :updated_at, :limit => number } }just a note .. request was for LAST two records… with this order query will return first two records, assume use :order => ''published_at desc'' :)>> BlogPost.latest > > then you can ask for e.g. BlogPost.latest 2 or BlogPost.latest 5 > (and with no number specified get them all in update order). > > FWIW, > -- > Hassan Schroeder ------------------------ hassan.schroeder-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org > http://about.me/hassanschroeder > twitter: @hassan > > -- > You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. > For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.-- ==============================================================================Tomas Meinlschmidt, MS {MCT, MCP+I, MCSE, AER}, NetApp Filer/NetCache www.meinlschmidt.com www.maxwellrender.cz www.lightgems.cz ============================================================================== -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Hassan Schroeder
2011-Aug-29 16:03 UTC
Re: Best form in pulling the last two records from a dataset in active record
On Sun, Aug 28, 2011 at 12:21 PM, Tom Meinlschmidt <tomas-ooGa/4BNRfTT2+6r9I86XQ@public.gmane.org> wrote:>> scope :latest, lambda {|number| { :order => :updated_at, :limit => number } } > > just a note .. request was for LAST two records… with this order query will return first two records, assume use > > :order => ''published_at desc''D''oh -- good point, thanks for catching that. And of course my example should have used ''published_at'' rather than ''updated_at''; fingers on autopilot! -- Hassan Schroeder ------------------------ hassan.schroeder-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org http://about.me/hassanschroeder twitter: @hassan -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.