patternrubyrailsMinor
Calculate sum of price on distant relation
Viewed 0 times
pricedistantcalculatesumrelation
Problem
I have a booking with a method
total which calculates the total price of the booking from the sum of all the prices of activities that belong to appointments. A booking has many appointments and the following seems to give the right answer, but it seems horrid. How can I improve this?def total
# TODO shouldn't this be a db query?
total = 0
self.appointments.each do |appointment|
unless appointment.activity.price.nil?
total += appointment.activity.price.to_f
end
end
total
endSolution
You can do this:
You could perhaps simplify it a little, by making an explicit 2nd order
Then you can just do:
In either case, it'll end up as a single DB-query.
def total
self.appointments.joins(:activity).sum(:price)
endYou 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 => :appointmentsThen you can just do:
def total
self.activities.sum(:price)
endIn either case, it'll end up as a single DB-query.
Code Snippets
def total
self.appointments.joins(:activity).sum(:price)
endhas_many :appointments
has_many :activities, :through => :appointmentsdef total
self.activities.sum(:price)
endContext
StackExchange Code Review Q#23204, answer score: 4
Revisions (0)
No revisions yet.