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
You could try just selecting the first result instead of all. ie. @tb = Innoculation.find( :first, etc... On 5/29/06, Craig White <craigwhite@azapple.com> wrote:> > 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 > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060529/262be536/attachment.html
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 On Mon, 2006-05-29 at 13:46 +1000, Daniel N wrote:> You could try just selecting the first result instead of all. > > ie. > @tb = Innoculation.find( :first, etc... > > On 5/29/06, Craig White <craigwhite@azapple.com> wrote: > 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 > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails
you likely have a has many relationship, therefore: personnel.innoculations.find(:first, :conditions => ["innoc_type ''TB''"], :order => ''innoc_date DESC'')[:innoc_date]
If you sort by date in a descending order then the first result will be that one with the last date. Hope this helps On 5/29/06, Craig White <craigwhite@azapple.com> wrote:> > 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 > > On Mon, 2006-05-29 at 13:46 +1000, Daniel N wrote: > > You could try just selecting the first result instead of all. > > > > ie. > > @tb = Innoculation.find( :first, etc... > > > > On 5/29/06, Craig White <craigwhite@azapple.com> wrote: > > 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 > > > > _______________________________________________ > > Rails mailing list > > Rails@lists.rubyonrails.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > _______________________________________________ > > Rails mailing list > > Rails@lists.rubyonrails.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060529/c8a940bb/attachment.html
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 Trier made it obvious buy yeah, thanks, I get it now. Must have been the Phoenix heat. Craig On Mon, 2006-05-29 at 14:31 +1000, Daniel N wrote:> If you sort by date in a descending order then the first result will > be that one with the last date. > > Hope this helps > > On 5/29/06, Craig White <craigwhite@azapple.com> wrote: > 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 > > On Mon, 2006-05-29 at 13:46 +1000, Daniel N wrote: > > You could try just selecting the first result instead of > all. > > > > ie. > > @tb = Innoculation.find( :first, etc... > > > > On 5/29/06, Craig White <craigwhite@azapple.com> wrote: > > 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 > > > > _______________________________________________ > > Rails mailing list > > Rails@lists.rubyonrails.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > _______________________________________________ > > Rails mailing list > > Rails@lists.rubyonrails.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails