Hey guys, I have a problem you''ll hopefully be able to help me with. I''m developing a logbook, and I have the following database structure: users (table) ---> flights (table) ---> flight_times (table) <--- flight_types (table) The flight_times are connected to flights with column flight_id, and to the flight_types via flight_type_id. flight_times (table) :flight_id :flight_type_id :hours (decimal) In order to fetch the flight_times with the highest hours count belonging to a specific flight, I use the following function: def total_time @flight = Flight.find(self.id) @flight_hours = FlightTime.maximum(:hours, :group => :flight) # FlightTime :belongs_to :flight @flight_hours[@flight].to_s end So far so good, it returns the value of the highest :hours count. HOWEVER, here starts the difficulty for me. The flight_types table has two columns: flight_types (table) :name :count_to_total (boolean) If the :count_to_total is false, I do not want it to be included when the total_time method looks for the maximum value. Did you get that? --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
maximum take most of the options that find does, eg :conditions and :joins Fred Sent from my iPhone On 10 Oct 2008, at 21:01, Alex <alex.e.jonsson-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Hey guys, I have a problem you''ll hopefully be able to help me with. > > I''m developing a logbook, and I have the following database structure: > > users (table) ---> flights (table) ---> flight_times (table) <--- > flight_types (table) > > The flight_times are connected to flights with column flight_id, and > to the flight_types via flight_type_id. > > flight_times (table) > :flight_id > :flight_type_id > :hours (decimal) > > In order to fetch the flight_times with the highest hours count > belonging to a specific flight, I use the following function: > > def total_time > @flight = Flight.find(self.id) > @flight_hours = FlightTime.maximum(:hours, :group => :flight) # > FlightTime :belongs_to :flight > @flight_hours[@flight].to_s > end > > So far so good, it returns the value of the highest :hours count. > > HOWEVER, here starts the difficulty for me. > > The flight_types table has two columns: > > flight_types (table) > :name > :count_to_total (boolean) > > If the :count_to_total is false, I do not want it to be included when > the total_time method looks for the maximum value. > > Did you get that? > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Thanks! Although your answer didn''t solve the issue at hand, it definitely put me on the right track! The solution I found was to use the :include key in the maximum method. This is what I ended up with: def total_time @flight = Flight.find(self.id) @flight_hours = FlightTime.maximum( :hours, :include => [:flight_type], :conditions => [''flight_types.count_to_total != "f"''], :group => :flight ) @flight_hours[@flight].to_s end On Oct 10, 11:24 pm, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> maximum take most of the options that find does, eg :conditions > and :joins > > Fred > > Sent from my iPhone > > On 10 Oct 2008, at 21:01, Alex <alex.e.jons...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > Hey guys, I have a problem you''ll hopefully be able to help me with. > > > I''m developing a logbook, and I have the following database structure: > > > users (table) ---> flights (table) ---> flight_times (table) <--- > > flight_types (table) > > > The flight_times are connected to flights with column flight_id, and > > to the flight_types via flight_type_id. > > > flight_times (table) > > :flight_id > > :flight_type_id > > :hours (decimal) > > > In order to fetch the flight_times with the highest hours count > > belonging to a specific flight, I use the following function: > > > def total_time > > @flight = Flight.find(self.id) > > @flight_hours = FlightTime.maximum(:hours, :group => :flight) # > > FlightTime :belongs_to :flight > > @flight_hours[@flight].to_s > > end > > > So far so good, it returns the value of the highest :hours count. > > > HOWEVER, here starts the difficulty for me. > > > The flight_types table has two columns: > > > flight_types (table) > > :name > > :count_to_total (boolean) > > > If the :count_to_total is false, I do not want it to be included when > > the total_time method looks for the maximum value. > > > Did you get that?--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Oct 11, 1:09 am, Alex Jonsson <alex.e.jons...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Thanks! Although your answer didn''t solve the issue at hand, it > definitely put me on the right track! > > The solution I found was to use the :include key in the maximum > method. This is what I ended up with: > > def total_time > @flight = Flight.find(self.id) > @flight_hours = FlightTime.maximum( :hours, > :include => [:flight_type], > :conditions => > [''flight_types.count_to_total != "f"''], > :group => :flight > ) > @flight_hours[@flight].to_s > end >You could make that rather more efficient by only considering the FlightTime for the flight you are interested in, rather than calculating it for all flights ever and throwing away the data for all but one of them. Fred> On Oct 10, 11:24 pm, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > wrote: > > > maximum take most of the options that find does, eg :conditions > > and :joins > > > Fred > > > Sent from my iPhone > > > On 10 Oct 2008, at 21:01, Alex <alex.e.jons...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > Hey guys, I have a problem you''ll hopefully be able to help me with. > > > > I''m developing a logbook, and I have the following database structure: > > > > users (table) ---> flights (table) ---> flight_times (table) <--- > > > flight_types (table) > > > > The flight_times are connected to flights with column flight_id, and > > > to the flight_types via flight_type_id. > > > > flight_times (table) > > > :flight_id > > > :flight_type_id > > > :hours (decimal) > > > > In order to fetch the flight_times with the highest hours count > > > belonging to a specific flight, I use the following function: > > > > def total_time > > > @flight = Flight.find(self.id) > > > @flight_hours = FlightTime.maximum(:hours, :group => :flight) # > > > FlightTime :belongs_to :flight > > > @flight_hours[@flight].to_s > > > end > > > > So far so good, it returns the value of the highest :hours count. > > > > HOWEVER, here starts the difficulty for me. > > > > The flight_types table has two columns: > > > > flight_types (table) > > > :name > > > :count_to_total (boolean) > > > > If the :count_to_total is false, I do not want it to be included when > > > the total_time method looks for the maximum value. > > > > Did you get that?--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---