# \[Newbie\] ActiveRecord: Calculate maximum of calculated column

**URL:** <https://www.ruby-forum.com/t/newbie-activerecord-calculate-maximum-of-calculated-column/184736>\
**Category:** Rails\
**Created:** [March 7, 2010, 5:58pm UTC](https://www.ruby-forum.com/t/newbie-activerecord-calculate-maximum-of-calculated-column/184736 "2010-03-07T17:58:54Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![fangheuschrecke](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/58956e/32.png) [@fangheuschrecke](https://www.ruby-forum.com/u/fangheuschrecke)\
**Post date:** [March 7, 2010, 5:58pm UTC](https://www.ruby-forum.com/t/newbie-activerecord-calculate-maximum-of-calculated-column/184736/1 "2010-03-07T17:58:54Z")

</div>

Hi there,

I recently started learning Ruby on Rails. I decided to learn Rails by  
programming some kind of small statistic application where I can  
document the fuel consumption of my car - i.e. everytime I’m at the gas  
station I log price and amount of the gas refuled and the distance  
travelled since the last refuelling.

One of my models looks like this:

# start

class Refuelling \< ActiveRecord::Base  
belongs\_to :car

def consumption  
liter/kilometer \* 100  
end

def liter\_price  
price/liter  
end

def type\_of\_tires  
if tires  
“winter tires”  
else  
“normal tires”  
end  
end  
end

# end

In my view I use this like this (simplyfied):

# start

\<% for r in @refuelling %\>  
\<%=h r.created\_at.to\_date %\>  
\<%=h r.kilometer %\>  
\<%=h r.liter %\>  
\<%=h r.price %\>  
\<%=h r.consumption %\>  
\<%=h r.liter\_price %\>  
\<%=h r.type\_of\_tires %\>  
\<% end %\>

# end

Now I want to calculate the max, min, average of this consumption  
method, but I do not know how. I tried creating a method like …

# start

def max  
maximum(consumption)  
end

# end

… in my model but that did not work.

Any hints on how to accomplish this?  
Thanks.

Max

---

<div class="post-metadata">

**Author:** ![fangheuschrecke](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/58956e/32.png) [@fangheuschrecke](https://www.ruby-forum.com/u/fangheuschrecke)\
**Post date:** [March 7, 2010, 6:11pm UTC](https://www.ruby-forum.com/t/newbie-activerecord-calculate-maximum-of-calculated-column/184736/2 "2010-03-07T18:11:48Z")

</div>

On Mar 7, 9:45 am, Max M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi there,
> 
> # start
> 
> def max  
> maximum(consumption)  
> end
> 
> # end

The activerecord maximum function maps straight to a sql MAX(), so  
can’t work on a calculated column like this. In general, unless you  
are willing to load all the corresponding ruby objects into memory and  
iterate over them, pretty much the only thing you can do is write a  
query that will do the whole thing database side.

You can do something like Refuelling.maximum(‘liter / distance’) but  
there’s not a straightforward way of avoiding defining the calculation  
in 2 places.

Fred
