Items.last is most recent?

If a model :has_many :items, can I rely on items.last being the most
recently created Item?
Or is this just a really bad idea?

Thanks,
-george

The ideal way to do this is the have the timestamp data in the
database. Then do an model.items.find(:first, :order => “created_at
DESC”).

If a model :has_many :items, can I rely on items.last being the most
recently created Item?
Or is this just a really bad idea?

Bad idea unless you add an :order => ‘created_at’ to that :has_many.

On Jun 30, 2008, at 11:13 AM, Philip H. wrote:

If a model :has_many :items, can I rely on items.last being the most
recently created Item?
Or is this just a really bad idea?

Bad idea unless you add an :order => ‘created_at’ to that :has_many.

Thanks. I didn’t know that could be done.

And in Rails 2.1, it’s easier

class Article < ActiveRecord::Base
named_scope :latest, :limit => 1, :order => “created_at DESC”
end

@latest = Article.latest

On Mon, Jun 30, 2008 at 12:43 PM, George B. [email protected]lid

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