Reputation: 685
I have a model that stores an integer every day. I need to add all these integers up, and then multiply the total by its price, which is stored in a different model and different for each user. In other words, imagine there are three records in the database:
Total Value = (Day1 * Day2 * Day3)*Price
This total value needs to be displayed on the users dashboard, so I don't think it's a number that needs to be stored in a database. How would I go about doing this in Rails?
Thank you!
Upvotes: 0
Views: 251
Reputation: 953
First of all, there are not enough information to help you. But if I understand, you have this schema (Assume that we have model called Entity):
date:date
sold:integer
So, if if you have such table, the query to calculate all entities should be like this:
@entities = Entity.all
@entities.sum(&:sold) * price #=> Price multiplied by total sum of sold.
If you need to sum only specified range, you can handle it like here: Rails - Filter by date range
Upvotes: 1