Multiple calculations on the same set of data: ruby or database?

Posted by Pierre on Stack Overflow See other posts from Stack Overflow or by Pierre
Published on 2010-12-25T18:14:17Z Indexed on 2010/12/25 20:54 UTC
Read the original article Hit count: 202

Filed under:
|
|

Hi,

I have a model Transaction for which I need to display the results of many calculations on many fields for a subset of transactions.

I've seen 2 ways to do it, but am not sure which is the best. I'm after the one that will have the least impact in terms of performance when data set grows and number of concurrent users increases.

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 }
...

Which one should I choose? (or is there a 3rd, better way?)

Thanks, P.

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about ruby