I have a model Transactionfor which I need to display the results of many calculations in many fields for a subset transactions.
I have seen 2 ways to do this, but I'm not sure which is better. I am for the one that will have the least impact in terms of performance when increasing the data set and increasing the number of concurrent users.
data[:total_before] = Transaction.where(xxx).sum(:amount_before)
data[:total_after] = Transaction.where(xxx).sum(:amount_after)
...
or
transactions = Transaction.where(xxx)
data[:total_before]= transactions.inject(0) {|s, e| s + e.amount_before }
data[:total_after]= transactions.inject(0) {|s, e| s + e.amount_after }
...
edit: where clause is always the same.
Which one to choose? (or is there a third, better way?)
Thanks P.
source
share