> 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.

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

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to 
rubyonrails-talk+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to