I have a model which has the created_on and updated_on fields. I would like to find the newest instances of my model from the database(for instance, last 10), but as I''m only 6 weeks into my Rails development, I''m not entirely sure how to do it. Any suggestions? Thanks, David
On Monday, June 19, 2006, at 7:59 PM, David M wrote:>I have a model which has the created_on and updated_on fields. I would >like to find the newest instances of my model from the database(for >instance, last 10), but as I''m only 6 weeks into my Rails development, I''m >not entirely sure how to do it. Any suggestions? > >Thanks, >David >_______________________________________________ >Rails mailing list >Rails@lists.rubyonrails.org >http://lists.rubyonrails.org/mailman/listinfo/railsThe easiest way to do this is with a find like this... @last_ten = Model.find(:all, :order=>''id DESC'', :limit=>10) technically this will only give you the last 10 created records. Since it orders by the id. You can get the last 10 updated like this.. @last_ten_updated = Model.find(:all, :order=>''updated_on DESC'', :limit=>10) _Kevin -- Posted with http://DevLists.com. Sign up and save your mailbox.
On 20 Jun 2006 00:29:52 -0000, Kevin Olbrich < devlists-rubyonrails@devlists.com> wrote:> > > The easiest way to do this is with a find like this... > > @last_ten = Model.find(:all, :order=>''id DESC'', :limit=>10) > > technically this will only give you the last 10 created records. Since > it orders by the id. > You can get the last 10 updated like this.. > > @last_ten_updated = Model.find(:all, :order=>''updated_on DESC'', > :limit=>10) > > _KevinIt''s probably not a good idea to rely on ids being in order if you have any aspirations for your app to scale (particularly to multiple database servers). Some approaches to this use different blocks of id numbers or alternating id numbers. Better to just always sort by created_at. -Sam -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060620/dbc9a62b/attachment.html
David M wrote:> I have a model which has the created_on and updated_on fields. I would > like to find the newest instances of my model from the database(for > instance, last 10), but as I''m only 6 weeks into my Rails development, > I''m > not entirely sure how to do it. Any suggestions? > > Thanks, > DavidTry this: Model.find(:all, :order => "updated_on DESC", :limit => 10) -- Posted via http://www.ruby-forum.com/.