# Find last

**URL:** <https://www.ruby-forum.com/t/find-last/95332>\
**Category:** Rails\
**Created:** [May 29, 2006, 5:38am UTC](https://www.ruby-forum.com/t/find-last/95332 "2006-05-29T05:38:58Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [May 29, 2006, 5:38am UTC](https://www.ruby-forum.com/t/find-last/95332/1 "2006-05-29T05:38:58Z")

</div>

This seems rather ugly. I am thinking that this needs to be in the model  
but I am looking to obtain the date for a tb test…

> > @tb = Innoculation.find(:all,  
> > :conditions =\> [“personnel\_id = 1 AND innoc\_type = ‘TB’”],  
> > :order =\> ‘innoc\_date DESC’)

=\> [#\<Innoculation:0xb77ed234 @attributes={“id”=\>“1”,  
“innoc\_type”=\>“TB”, “personnel\_id”=\>“1”, “innoc\_date”=\>“2006-05-27”}\>]

> > @last\_tb = @tb[0][:innoc\_date]  
> > =\> #\<Date: 4907765/2,0,2299161\>

which says to me that in the personnel model, it should be something  
like this…

def last\_tb personnel\_id  
@tb = Innoculation.find(:all,  
:conditions =\> ["personnel\_id = ? AND innoc\_type = ‘TB’,  
personnel\_id "],  
:order =\> ‘innoc\_date DESC’)  
@last\_tb = @tb[0][:innoc\_date]  
end

Is there a simpler way?

Craig

---

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [May 29, 2006, 5:48am UTC](https://www.ruby-forum.com/t/find-last/95332/2 "2006-05-29T05:48:23Z")

</div>

You could try just selecting the first result instead of all.

ie.  
@tb = Innoculation.find( :first, etc…

---

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [May 29, 2006, 5:57am UTC](https://www.ruby-forum.com/t/find-last/95332/3 "2006-05-29T05:57:44Z")

</div>

well there is bound to be more than 1 and I want the one with the latest  
date. First doesn’t seem to ensure that result.

Craig

---

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [May 29, 2006, 6:34am UTC](https://www.ruby-forum.com/t/find-last/95332/4 "2006-05-29T06:34:42Z")

</div>

If you sort by date in a descending order then the first result will be  
that  
one with the last date.

Hope this helps

---

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [May 29, 2006, 6:10am UTC](https://www.ruby-forum.com/t/find-last/95332/5 "2006-05-29T06:10:01Z")

</div>

you likely have a has many relationship, therefore:

personnel.innoculations.find(:first, :conditions =\> [“innoc\_type =  
‘TB’”], :order =\> ‘innoc\_date DESC’)[:innoc\_date]

---

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [May 29, 2006, 5:18pm UTC](https://www.ruby-forum.com/t/find-last/95332/6 "2006-05-29T17:18:12Z")

</div>

Sometimes I can’t draw a straight line with a ruler in my hand. I didn’t  
get that when you said it but Michael T. made it obvious buy yeah,  
thanks, I get it now. Must have been the Phoenix heat.

Craig
