破坏的协会路轨4

时间:2017-05-30 13:27:08

标签: ruby-on-rails ruby-on-rails-4 activerecord

我的模特是投标,拍卖和公司。我与Bid的{​​{1}}关联似乎已被打破。出于某种原因,Auction正在给我

Auction.joins(:bids).where(bids: @bids).to_sql

我感到困惑的是查询具有条件"SELECT "auctions".* FROM "auctions" INNER JOIN "bids" ON "bids"."auction_id" = "auctions"."id" WHERE "auctions"."auction_id" IN (SELECT "bids"."id" FROM "bids" INNER JOIN "inventory_parts" ON "bids"."inventory_part_id" = "inventory_parts"."id" WHERE "inventory_parts"."company_id" = 1)" 的原因。它应该是WHERE "auctions"."auction_id"

为了简洁起见,我将仅列出我认为在我的问题中扮演角色的模型以及我认为重要的协会

我有拍卖模式

WHERE "auctions"."id"

投标模型

class Auction < ActiveRecord::Base
  belongs_to :company
  has_one :auction_part, dependent: :destroy
  has_one :part, through: :auction_part
  has_many :bids, dependent: :destroy

和公司模式

class Bid < ActiveRecord::Base
  has_one :company, through: :inventory_part
  belongs_to :auction
  belongs_to :inventory_part

1 个答案:

答案 0 :(得分:1)

您需要在bids.id IN (1,2)上运行查询。它应该是

Auction.joins(:bids).where(bids: {id: @bids})

# OR 

Auction.joins(:bids).where(bids: {id: @bids.pluck(:id)})