为什么列 trades.item_id 不存在?

why does column trades.item_id not exist?

我有一个关系模型,其中两个用户可以进行交易以交换两个项目。

class User < ActiveRecord::Base
 has_many :owned_items, class_name: "Item"
 has_many :trades_received, class_name: "Trade", through: :owned_items, source: :trades
 has_many :trades
 has_many :wanted_items, class_name: "Item", through: :trades, source: :item
end

class Item < ActiveRecord::Base
 belongs_to :owner, class_name: "User", foreign_key: :user_id
 has_many :trades, dependent: :destroy
 has_many :trade_requesters, through: :trades
 has_many :trade_recipients, through: :trades
end

class Trade < ActiveRecord::Base
 belongs_to :trade_requester, class_name: "User"
 belongs_to :trade_recipient, class_name: "User"
 belongs_to :wanted_item, class_name: "Item", foreign_key: :wanted_item_id
 belongs_to :collateral_item, class_name: "Item", foreign_key: :collateral_item_id
end

我的交易 table 的迁移如下所示:

create_table :trades do |t|
 t.belongs_to :trade_requester
 t.belongs_to :trade_recipient
 t.belongs_to :wanted_item
 t.belongs_to :collateral_item
end

堆栈跟踪指向我用来列出所有交易请求的辅助方法。该行表示 @trades = current_user.trades_received.requested.count,然后向下到用户 has_many :owned_items, class_name: "Item" 上的模型关联。据我了解,看起来 trades_received 方法(称为 through: :owned_itemssource: :trades 应该在迁移中引用 :wanted_item_id 外键。但事实并非如此。如果我创建一个迁移以添加 item_id,它会起作用,但是一个 Trade 需要两个项目,所以我将它分成两个 wanted_itemcollateral_item 关联。我如何设置该用户关联,以便它引用另一个用户正在请求的项目? Items has_many :trades 应该按照我的方式,还是 Items belongs_to :trades?

完整错误:

PG::UndefinedColumn: ERROR:  column trades.item_id does not exist
LINE 1: ...LECT COUNT(*) FROM "trades" INNER JOIN "items" ON "trades"."...
                                                         ^
: SELECT COUNT(*) FROM "trades" INNER JOIN "items" ON "trades"."item_id" = "items"."id" WHERE "items"."user_id" =  AND "trades"."approved" IS NULL

tldr:我需要跟踪一堆复杂的 has_many :through 关联,我认为我的数据模型不正确,需要帮助理解原因。谢谢。

好的,好的。问题在这里:

has_many :trades, dependent: :destroy

在您的 Trade 模型中:

belongs_to :wanted_item, ...
belongs_to :collateral_item, ..

Rails 无法自动处理。

您需要执行以下步骤之一(取决于您在应用中的需要):

如果您需要单独的关联:

class User < ActiveRecord::Base
  has_many :trades_received, class_name: "Trade", through: :owned_items, source: :wantable_trades
end

class Item < ActiveRecord::Base
  has_many :wanted_trades, class_name: 'Trade', inverse_of: :wanted_item, dependent: :destroy
  has_many :collateral_trades, class_name: 'Trade', inverse_of: :collateral_item, dependent: :destroy
end

如果您需要所有交易作为单一关联:

好吧,你会很头疼的:) 在这种情况下,你应该手动 select 关联,或者重新考虑你的数据模型。

您正在 UserItem 之间建立两个 has_many :through 关系,Trade 作为两者的联接 table。你有一些关系混淆。这是基于您的迁移的设置:

class User < ActiveRecord::Base
 has_many :received_trades, class_name: "Trade", foreign_key: "trade_recipient"
 has_many :requested_trades, class_name: "Trade", foreign_key: "trade_requester"
 has_many :collateral_items, through: :received_trades
 has_many :wanted_items, through: :requested_trades
end

class Item < ActiveRecord::Base
 has_many :collateral_items, class_name: "Trade", foreign_key: "collateral_item"
 has_many :wanted_items, class_name: "Trade", foreign_key: "wanted_item"
 has_many :trade_requesters, through: :wanted_items
 has_many :trade_recipients, through: :collateral_items
end

class Trade < ActiveRecord::Base
 belongs_to :trade_requester, class_name: "User"
 belongs_to :trade_recipient, class_name: "User"
 belongs_to :wanted_item, class_name: "Item"
 belongs_to :collateral_item, class_name: "Item"
end

##migration
create_table :trades do |t|
 t.belongs_to :trade_requester
 t.belongs_to :trade_recipient
 t.belongs_to :wanted_item
 t.belongs_to :collateral_item
end

一些解释:

Item has_many :collateral_item  ## item_id in table collateral_items
Item has_many :collateral_item, class_name: "Trade", foreign_key: "collateral_item"
##collateral_item_id in trades table.