Ruby on Rails 프로젝트 4.0에서 has_many
Transactions
모델 인 Payments
모델이 있습니다.특정 조건과 일치하는 모델이있는 모델의 합계 속성
나는 지금 다음 데이터
SELECT * FROM payments;
id | paid_amount
---+--------------
1 | 200
2 | 300
3 | 100
4 | 400
5 | 100
6 | 600
7 | 100
8 | 100
9 | 800
SELECT * FROM transactions;
id | payment_id | type
---+-------------+------
1 | 2 | cash
2 | 3 | credit
3 | 1 | credit
4 | 4 | cash
5 | 1 | cash
6 | 6 | credit
7 | 1 | cash
8 | 1 | credit
9 | 8 | cash
이있는 경우
> Payment.where(id: Transaction.where(type: 'cash').pluck(:payment_id)).sum(:paid_amount)
> 1000
> # The sum of paid_amount from payments with ids 1, 2, 4 and 8
을 다음과 같이 내가, 특정 유형의 트랜잭션을 가지고
Payments
에서 숫자 속성
paid_amount
의 합을 계산 해요
하지만 수천 개의 레코드로는 충분히 빠르지 않아서 행운없이 includes
으로이 작업을 수행하려고했습니다.
> Payment.includes(:transactions).where("transactions.type = 'cash'").sum(:paid_amount)
> 1200
> # paid_amount of payment with id 1 is considered two times because of transactions with id 5 and 7
내가 필요한 번호를 어떻게 계산해야합니까?