2017-05-30 47 views
0

我的模特是出價,拍賣和公司。我的Bid協會到Auction似乎被打破。出於某種原因,Auction.joins(:bids).where(bids: @bids).to_sql是給我破碎的協會Rails 4

"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 

和公司型號

class Company < ActiveRecord::Base 
    has_secure_password 
    has_many :auctions, dependent: :destroy 
    has_many :bids, through: :inventory_parts 
    has_many :inventory_parts, dependent: :destroy 

回答

1

您需要查詢在bids.id IN (1,2)上運行。它應該是

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

# OR 

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

因此,'Auction.joins(:bids).where(bids:@bids)'是錯誤的?不是協會? – gemart

+0

是的,因爲無論何時你通過'where(column:value)'rails找到當前模型表中的列。所以,在你的情況'拍賣' –

+0

和'出價:@出價'這是奇怪的查詢的可能原因 –