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?
on 2008-10-10 22:01
on 2008-10-11 00:12
maximum take most of the options that find does, eg :conditions and :joins Fred Sent from my iPhone
on 2008-10-11 02:09
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...@gmail.com>
on 2008-10-11 02:52
On Oct 11, 1:09 am, Alex Jonsson <alex.e.jons...@gmail.com> wrote: > :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