Find last


#1

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


#2

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

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


#3

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


#4

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

Hope this helps


#5

you likely have a has many relationship, therefore:

personnel.innoculations.find(:first, :conditions => [“innoc_type =
‘TB’”], :order => ‘innoc_date DESC’)[:innoc_date]


#6

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