我正尝试通过多个自定义关联加入同一个表。
我的模型有以下关联,
class Order < ApplicationRecord
belongs_to :advertiser, class_name: 'Account'
belongs_to :facilitator, class_name: 'Account'
belongs_to :creator, class_name: 'Account'
如何使用Active Record和/或Arel创建以下SQL查询?(不仅仅是直接提供SQL字符串)
INNER JOIN accounts ON accounts.id = orders.facilitator_id
OR accounts.id = orders.creator_id
OR accounts.id = orders.advertiser_id
我试过以下几种方法,
Order.joins(:advertiser, :facilitator, :creator)
但得到了以下结果
SELECT `orders`.*
FROM `orders`
INNER JOIN `accounts` ON `accounts`.`id` = `orders`.`advertiser_id`
INNER JOIN `accounts` `facilitators_orders` ON `facilitators_orders`.`id` = `orders`.`facilitator_id`
INNER JOIN `accounts` `creators_orders` ON `creators_orders`.`id` = `orders`.`creator_id`
1条答案
按热度按时间yftpprvb1#
您需要指定
has_many
关系Account
模型的外键。在
Account
模型中类似的问题可能也有帮助!Multiple Associations With the Same Table rails