Find 'newest' of a model

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

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

Try this:
Model.find(:all, :order => “updated_on DESC”, :limit => 10)

On 20 Jun 2006 00:29:52 -0000, Kevin O. <
[email protected]> wrote:

@last_ten_updated = Model.find(:all, :order=>‘updated_on DESC’,
:limit=>10)

_Kevin

It’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

On Monday, June 19, 2006, at 7:59 PM, David M wrote:

http://lists.rubyonrails.org/mailman/listinfo/rails
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)

_Kevin

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs