The Whiz of Oz
The Whiz of Oz

Reputation: 7043

How to refresh a method in Rails model on data update?

I have a method in my Transaction model which calculates sum of data from Transactions table:

def self.total_amount
 sum('amount')
end

It is displayed in the view

<%= Transaction.total_amount %>

After the data is refreshed (new transactions added) the sum stays the same. How do I make it recalculate itself?

Thanks

Upvotes: 0

Views: 252

Answers (1)

user419017
user419017

Reputation:

Try:

def self.total_amount
  lambda { sum('amount') }.call
end

Though, as touched on in the comments, I have no idea why you're experiencing such behaviour. I've checked in a production environment and I receive no such problem.

Upvotes: 1

Related Questions