2017-05-30 10 views
0

私のモデルは入札、オークション、および会社です。私のBidAuctionへの関連が壊れているようです。何らかの理由Auction.joins(:bids).where(bids: @bids).to_sqlのために私が混乱している何を私に壊れた関連付けのレール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

そして '入札:@入札'これは奇妙なクエリの考えられる理由です –

関連する問題