Forum: Ruby on Rails Find last

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Craig W. (Guest)
on 2006-05-29 07:38
(Received via mailing list)
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
Daniel -. (Guest)
on 2006-05-29 07:48
(Received via mailing list)
You could try just selecting the first result instead of all.

ie.
@tb = Innoculation.find( :first, etc...
Craig W. (Guest)
on 2006-05-29 07:57
(Received via mailing list)
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
Michael T. (Guest)
on 2006-05-29 08:10
(Received via mailing list)
you likely have a has many relationship, therefore:

personnel.innoculations.find(:first, :conditions => ["innoc_type =
'TB'"], :order => 'innoc_date DESC')[:innoc_date]
Daniel -. (Guest)
on 2006-05-29 08:34
(Received via mailing list)
If you sort by date in a descending order then the first result will be
that
one with the last date.

Hope this helps
Craig W. (Guest)
on 2006-05-29 19:18
(Received via mailing list)
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
This topic is locked and can not be replied to.