T
T
The Whiz2013-10-29 18:25:55
Ruby on Rails
The Whiz, 2013-10-29 18:25:55

Math calculations in Rails - what's the best way to do it?

Please tell me the best way to organize and perform calculations in ROR(4). Let me explain: I have a table with numbers, and I want to perform different actions on them. I don’t really want to sculpt into a controller , because in a month my controller will turn into a mess. I also want the result of the calculation to be updated as new numbers are added/subtracted. Separate class? Module? If one of the Khabrovites gives an example of calculating the sum of a table (as above) at the model level, I will be very grateful.
@total = Transaction.sum("amount")<br>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
Loremaster, 2013-10-29
@modernstyle

You need to try to put the logic into the appropriate model and keep the controller thin so as not to breed porridge there. Try writing the following in the model:

def self.total_amount
 sum('amount')
end

And call later ModelName.total_amount.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question