# The maximum value issue

**URL:** <https://www.ruby-forum.com/t/the-maximum-value-issue/150654>\
**Category:** Rails\
**Created:** [October 10, 2008, 10:01pm UTC](https://www.ruby-forum.com/t/the-maximum-value-issue/150654 "2008-10-10T22:01:33Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![alex](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/db5fbb/32.png) [@alex](https://www.ruby-forum.com/u/alex)\
**Post date:** [October 10, 2008, 10:01pm UTC](https://www.ruby-forum.com/t/the-maximum-value-issue/150654/1 "2008-10-10T22:01:33Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![alex](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/db5fbb/32.png) [@alex](https://www.ruby-forum.com/u/alex)\
**Post date:** [October 11, 2008, 12:12am UTC](https://www.ruby-forum.com/t/the-maximum-value-issue/150654/2 "2008-10-11T00:12:52Z")

</div>

maximum take most of the options that find does, eg :conditions  
and :joins

Fred

Sent from my iPhone

---

<div class="post-metadata">

**Author:** ![alex](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/db5fbb/32.png) [@alex](https://www.ruby-forum.com/u/alex)\
**Post date:** [October 11, 2008, 2:09am UTC](https://www.ruby-forum.com/t/the-maximum-value-issue/150654/3 "2008-10-11T02:09:54Z")

</div>

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 C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![alex](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/db5fbb/32.png) [@alex](https://www.ruby-forum.com/u/alex)\
**Post date:** [October 11, 2008, 2:52am UTC](https://www.ruby-forum.com/t/the-maximum-value-issue/150654/4 "2008-10-11T02:52:35Z")

</div>

On Oct 11, 1:09 am, Alex J. [removed\_email\_address@domain.invalid](mailto: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.

Fred
