Active Record Association Methods
Published:
March 06, 2009
I often forget that you can define methods for an Active Record associations in a block after the association declaration like so:
class Order < ActiveRecord::Base
has_many :items, :dependent => :destroy do
def total
inject(0) {|sum, s| sum += s.price}
end
end
end
>> @order = Order.last
>> @order.items.total
=> 27.45
Most of the time this makes more sense than defining them as instance methods in the parent model and much more sense than class methods in the child because it will require the association to work anyhow (perhaps though I haven’t chosen the best example to illustrate this as you could potentially use an Item.total method independently eg. Item.all.total, but you get the picture).