# Items.last is most recent?

**URL:** <https://www.ruby-forum.com/t/items-last-is-most-recent/141811>\
**Category:** Rails\
**Created:** [June 30, 2008, 7:11pm UTC](https://www.ruby-forum.com/t/items-last-is-most-recent/141811 "2008-06-30T19:11:11Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![george\_b](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ecb155/32.png) [@george\_b](https://www.ruby-forum.com/u/george_b)\
**Post date:** [June 30, 2008, 7:11pm UTC](https://www.ruby-forum.com/t/items-last-is-most-recent/141811/1 "2008-06-30T19:11:11Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![george\_b](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ecb155/32.png) [@george\_b](https://www.ruby-forum.com/u/george_b)\
**Post date:** [June 30, 2008, 7:14pm UTC](https://www.ruby-forum.com/t/items-last-is-most-recent/141811/2 "2008-06-30T19:14:07Z")

</div>

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”).

---

<div class="post-metadata">

**Author:** ![george\_b](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ecb155/32.png) [@george\_b](https://www.ruby-forum.com/u/george_b)\
**Post date:** [June 30, 2008, 7:15pm UTC](https://www.ruby-forum.com/t/items-last-is-most-recent/141811/3 "2008-06-30T19:15:17Z")

</div>

> 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.

---

<div class="post-metadata">

**Author:** ![george\_b](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ecb155/32.png) [@george\_b](https://www.ruby-forum.com/u/george_b)\
**Post date:** [June 30, 2008, 7:44pm UTC](https://www.ruby-forum.com/t/items-last-is-most-recent/141811/4 "2008-06-30T19:44:36Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![george\_b](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ecb155/32.png) [@george\_b](https://www.ruby-forum.com/u/george_b)\
**Post date:** [June 30, 2008, 10:25pm UTC](https://www.ruby-forum.com/t/items-last-is-most-recent/141811/5 "2008-06-30T22:25:04Z")

</div>

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. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
