Let’s say I have an ActiveRecord called Apples, and I want a class method that calculates the total price of every apple in my database, as so:
def Apples.total_price
Apples.sum(:price)
end
This is a method I use in one of my views to make a pie chart. So, something like:
Apples.brand(“red delicious”).sum(:price)/Apples.total_price = % of pie chart
Apples.brand(“fuji”).sum(:price)/Apples.total_price = another % of pie chart
Apples.total_price is called repeatedly but the value isn’t going to change at this point. A) Does Rails make the query repeatedly, or does it know to cache the value? And if it does call it repeatedly, what’s the best way to define a method in Apple so that this total_price is just calculated once for the runtime of this view?
The way you’ve defined it, I believe it is done repeatedly. The logs will show for sure.
Through a technique known as memoization you process the it only if needed. Rails supplies easy memoization for instance methods, but you’re on your own for class methods.
Here’s how you roll your own memoization for a class method.