0
オブジェクトの配列から和の配列を作成し、次のようになりますルビー:私はオブジェクトの配列を持っている
[#<Holding id: 5, user_id: 21, outcome_id: 7, quantity: 200, cost_basis: nil, created_at: "2011-12-15 20:52:05", updated_at: "2011-12-15 20:52:05">,
#<Holding id: 6, user_id: 21, outcome_id: 7, quantity: 750, cost_basis: nil, created_at: "2011-12-15 21:31:30", updated_at: "2011-12-15 21:31:30">,
#<Holding id: 7, user_id: 21, outcome_id: 9, quantity: 1231, cost_basis: nil, created_at: "2011-12-15 21:35:31", updated_at: "2011-12-15 21:35:31">]
私は私のモデルのfind_all_by_user_idを使用してこれを取得した(Holding.find_all_by_user_id(21)) 。しかし、私が必要とするのは、合計の配列です。ですから、例えば、私は、この例では代わりに取得したいのですがすることです:私は私のアウトカムモデルOutcome.descriptionから引き出して、この第二に、それを持っていることができれば、それはさらに良いですになるだろう何
[#<Holding id: 5, user_id: 21, outcome_id: 7, quantity: 950, cost_basis: nil, created_at: "2011-12-15 20:52:05", updated_at: "2011-12-15 20:52:05">,
#<Holding id: 7, user_id: 21, outcome_id: 9, quantity: 1231, cost_basis: nil, created_at: "2011-12-15 21:35:31", updated_at: "2011-12-15 21:35:31">]
idと共に配列。ループして、outcome_idと一致するか、より良い方法がある場合は合計を作成するだけですか?
私はそれをSQLで行うことがより良い解決策であることに同意します。これは、常に役立つデータが少なく、DBMはそのような持ち上げのために作られています。私が行全体を取得する唯一の時間は、合計に基づいてデータの後続の処理を行う必要がある場合です。 –