2016-08-01 58 views
0

我試圖通過生成Rails的這條SQL語句:在Rails中,如何做一個複雜的嵌套連接

SELECT * FROM reward_cards 
INNER JOIN reward_card_payments 
    ON reward_card_payments.reward_card_id = reward_cards.id 
INNER JOIN payments 
    ON payments.id = reward_card_payments.payment_id AND payments.amount < 0; 

沒有 '軌道' 的方式與此類似:

RewardsCard.joins(reward_card_payments => {:payments => payments.amount < 0}) 

回答

0

我能找到的最佳方式是

RewardsCard.joins(:reward_card_payments).joins(
    'INNER JOIN payments 
    ON payments.id = reward_card_payments.payment_id AND payments.amount < 0' 
)