在Rails中,如何进行复杂的嵌套连接

时间:2016-08-01 17:38:14

标签: ruby-on-rails activerecord

我正在尝试通过Rails生成这个SQL语句:

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;

是否存在类似于此的'rails'方式:

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

1 个答案:

答案 0 :(得分:0)

我能找到的最佳方式是

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