# Math between different records

**URL:** <https://www.ruby-forum.com/t/math-between-different-records/205487>\
**Category:** Rails\
**Created:** [April 7, 2011, 1:45pm UTC](https://www.ruby-forum.com/t/math-between-different-records/205487 "2011-04-07T13:45:23Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dubstep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/cdc98d/32.png) [@dubstep](https://www.ruby-forum.com/u/dubstep)\
**Post date:** [April 7, 2011, 1:45pm UTC](https://www.ruby-forum.com/t/math-between-different-records/205487/1 "2011-04-07T13:45:23Z")

</div>

Hi all.

Could you please recommend elegant way (read - best practices in  
“Rails way”, less DB calls) to solve the following situation ?

I have 2 tables:

Meter

- has\_many :meter\_records

MeterRecord

- belong\_to :meter

I have to calculate difference between meter’s current month record  
and same meter’s previous month record.

I would like to avoid unnecessary selects from DB and also avoid  
dealing with SQL - I prefer functionality provided by ActiveRecord.

Can you please recommend the solution or at least point me to a good  
tutorial ?

Thank you in advance.

Artūras

---

<div class="post-metadata">

**Author:** ![SSArtSSrasB.SS\_SSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/8e8cbc/32.png) [@SSArtSSrasB.SS\_SSS](https://www.ruby-forum.com/u/SSArtSSrasB.SS_SSS)\
**Post date:** [April 7, 2011, 5:54pm UTC](https://www.ruby-forum.com/t/math-between-different-records/205487/2 "2011-04-07T17:54:54Z")

</div>

> I have to calculate difference between meter’s current month record  
> and same meter’s previous month record.
> 
> I would like to avoid unnecessary selects from DB and also avoid  
> dealing with SQL - I prefer functionality provided by ActiveRecord.
> 
> Can you please recommend the solution or at least point me to a good  
> tutorial ?
> 
> Thank you in advance.

No idea if this is best practice, but I’d put it in a method in the  
Meter model and just do it…

class Meter…

def meth  
c, l = meter\_records.order(‘created\_at desc’).limit(2)  
c.some\_field - l.some\_field  
end

end

I’m making some assumptions about your models of course, but the above  
should get you started.

-philip

---

<div class="post-metadata">

**Author:** ![SSArtSSrasB.SS\_SSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/8e8cbc/32.png) [@SSArtSSrasB.SS\_SSS](https://www.ruby-forum.com/u/SSArtSSrasB.SS_SSS)\
**Post date:** [April 8, 2011, 6:26pm UTC](https://www.ruby-forum.com/t/math-between-different-records/205487/3 "2011-04-08T18:26:23Z")

</div>

“ArtūrasB.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote in post #991441:

> Could you please recommend elegant way (read - best practices in  
> “Rails way”, less DB calls) to solve the following situation ?
> 
> I have 2 tables:
> 
> Meter
> 
> - has\_many :meter\_records
> 
> MeterRecord
> 
> - belong\_to :meter
> 
> I have to calculate difference between meter’s current month record  
> and same meter’s previous month record.
> 
> I would like to avoid unnecessary selects from DB and also avoid  
> dealing with SQL - I prefer functionality provided by ActiveRecord.

Having some experience with this sort of thing, I would consider adding  
a summary table to your database. It may seem counter-intuitive since  
we’re aways told to avoid data duplication, but in the case of reporting  
I often make this exception. There can be incredible performance  
benefits to be gained by flattening, and pre-calculating data used in  
reports.

I would create a simple flat table that records monthly summations for  
each customer. This makes reporting fast and efficient. I would keep the  
normalized data as well for showing detailed transaction reports. You’ll  
need good automated tests to ensure your model logic always keeps the  
data in sync, and possibly a batch process for regenerating the summary  
data in case it ever does get out of sync.

This may be overkill for your application. It really depends on how many  
customer records you expect to have. The implementation is pretty  
straight forward. Every time you add a new meter reading, you also  
update the summary. The safest way to do that is to use the AR aggregate  
features to sum the details records and replace the summary value. As  
opposed to adjusting the sum by adding the value to the previous sum.  
This should be fast enough to be unnoticeable to the user when performed  
on an individual customer’s records. Also, don’t forget to adjust the  
summation for updates and deletes along with inserts.

This could even be implemented as database triggers if you want to  
ensure that this happens no matter whether it’s your application doing  
the updating or some external application performing the changes.
