Calculate sum of price on distant relation
You can do this:

def total
  self.appointments.joins(:activity).sum(:price)
end


You could perhaps simplify it a little, by making an explicit 2nd order has_many on your booking model:

has_many :appointments
has_many :activities, :through =&gt; :appointments


Then you can just do:

def total
  self.activities.sum(:price)
end


In either case, it'll end up as a single DB-query.