Forum: Ruby on Rails The maximum value issue

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Alex (Guest)
on 2008-10-11 00:01
(Received via mailing list)
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)
: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(
    @flight_hours = FlightTime.maximum(:hours, :group => :flight) #
FlightTime :belongs_to :flight

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)
: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?
Frederick C. (Guest)
on 2008-10-11 02:12
(Received via mailing list)
maximum take most of the options that find does, eg :conditions
and :joins


Sent from my iPhone
Alex J. (Guest)
on 2008-10-11 04:09
(Received via mailing list)
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(
    @flight_hours = FlightTime.maximum( :hours,
                                        :include => [:flight_type],
                                        :conditions =>
['flight_types.count_to_total != "f"'],
                                        :group => :flight

On Oct 10, 11:24 pm, Frederick C. <removed_email_address@domain.invalid>
Frederick C. (Guest)
on 2008-10-11 04:52
(Received via mailing list)
On Oct 11, 1:09 am, Alex J. <removed_email_address@domain.invalid> 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.

This topic is locked and can not be replied to.