Has_many: :through for LineItems - Rails

Has_many: :through for LineItems - Rails

我有四个模型:

用户:

has_many :listings
has_many :orders

清单:

belongs_to :seller, class_name: "User", foreign_key: :user_id
has_many :order_groups, through: :orders
has_many :orders

订单:

has_one :seller, through: :listing 
belongs_to :listing
belongs_to :order_group

订单组:

has_many :listings, through: :orders
has_many :orders
has_many :sellers, through: :orders

当我尝试拉 Order.where(seller: User.find(3)) 时,我得到一个空集合。但是,当我执行 Order.last.seller 时,我得到了卖家的 user_id.

如何拉Order.where(seller: User.find(3))'

您可以将查询写为

Order.joins(:listing).where('listings.user_id = ?', 3)