Answer the question
In order to leave comments, you need to log in
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
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
ModelName.total_amount
.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question