我的模型是出价,拍卖和公司。我的Bid与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和公司模型
class Company < ActiveRecord::Base
has_secure_password
has_many :auctions, dependent: :destroy
has_many :bids, through: :inventory_parts
has_many :inventory_parts, dependent: :destroy发布于 2017-05-30 13:32:45
您需要在bids.id IN (1,2)上运行该查询。它应该是
Auction.joins(:bids).where(bids: {id: @bids})
# OR
Auction.joins(:bids).where(bids: {id: @bids.pluck(:id)})https://stackoverflow.com/questions/44263617
复制相似问题