I'd like to list bakeries by the number pastries they have on sale, say.
To rank them, I would sum the multiplication of the number of pastries available of each pastry, by the percentage they are on sale. Bakery could have 0-10 pastries on sale. For example:
bakery1.pastry1 = 100
bakery1.pastry1_discount = 10 (%)
bakery1.pastry2 = 50
bakery1.pastry2_discount = 20 (%)
(2 pastries on sale)
(100 x 10) + (50 x 20) = 2000 .. would be listed first
bakery2.pastry1 = 10
bakery2.pastry1_discount = 40
bakery2.pastry2 = nil
bakery2.pastry2_discount = nil
(only 1 pastry on sale, etc)
10 x 40 = 400 .. listed 2nd
bakery3.pastry1 = 10
bakery3.pastry1_discount = 20
bakery3.pastry2 = nil
bakery3.pastry2_discount = nil
10 x 20 = 200 .. listed last
So how do I order Bakery by the sum of the multiplication of certain attribute values?
I understand thus far that sort_by is heavy on the memory of the database.
UPDATE: ... it'd have to be (pastries available - pastries sold) x discount
As I understand the problem. Here is the solution to this,
Example