Let's say I have an ActiveRecord called Apples, and I want the class method to calculate the total value of each apple in my database like this:
def Apples.total_price
Apples.sum(:price)
end
This is the method that I use in one of my views to create a pie chart. So, something like: Apples.brand ("red delicious"). Sum (: price) /Apples.total_price =% Pie Chart
Apples.brand ("fuji"). sum (: price) /Apples.total_price = another% pie chart
Apples.total_price is called repeatedly, but the value will not change at the moment. A) Does Rails repeat the request, or does it know to cache the value? And if he calls it repeatedly, then what is the way to define a method in Apple so that this total_price is only calculated once for the execution time of this view?
Zando source
share