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 --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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"). On Jun 30, 11:09 am, George Bailey <listcatc...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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? > > Thanks, > -george--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@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 -~----------~----~----~----~------~----~------~--~---
> 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. --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Jun 30, 2008, at 11:13 AM, Philip Hallstrom 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. --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 Bailey <listcatcher-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > > On Jun 30, 2008, at 11:13 AM, Philip Hallstrom 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. > > > > >--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---