Re: [Rails] 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@ruby-forum.com> 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 } }
> 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@gmail.com
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@googlegroups.com.
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.
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home