sql - Can Rails array be "unzipped"? -


i using following queries in customer model call obtain desired set of transactions.

  transactions = sub_account.transactions   transaction_items = transactions.map{|transaction| [transaction.transaction_items]} 

however, returning array of array of hashes. rails console

[ [# <transactionitem id: 29, amount: 20>, #<transactionitem id: 35, amount: 40>],<br>   [# <transactionitem id: 31, amount: 30>, #<transactionitem id: 38, amount: 30>],<br>   [# <transactionitem id: 43, amount: 30>, #<transactionitem id: 21, amount: 40>],<br> ] 

this process works well. trying run query on transaction_items can't becuase they're embedded in array. here final desired query unable run.

transaction_items.where(:amount => 30).sum("amount") 

i know can zip array, can unzip it? can't find documentation on it. if no unzip, can adapt query work on embedded arrays?

thanks.

what about:

transactions_items = [] transactions.each{|n|transactions_items = transactions_items.concat(n.transaction_items)} 

assuming transactions.transactions_items array. .each applies block each item, concats transactions_items of current element n array transactions_items.

and

sum = 0 tosum = transactions_items.where(:amount => 30) tosum.each{|transaction_item|sum += transaction_item.amount} 

or

sum = 0 tosum = transactions_items.where(:amount => 30) tosum.inject{|sum, transaction_item| sum + transaction_item.amount} 

see how sum array of numbers in ruby?


Comments

Popular posts from this blog

html5 - What is breaking my page when printing? -

c# - must be a non-abstract type with a public parameterless constructor in redis -

ajax - PHP/JSON Login script (Twitter style) not setting sessions -