Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 为什么列trades.item_id不存在?_Ruby On Rails_Ruby_Postgresql_Has Many Through_Belongs To - Fatal编程技术网

Ruby on rails 为什么列trades.item_id不存在?

Ruby on rails 为什么列trades.item_id不存在?,ruby-on-rails,ruby,postgresql,has-many-through,belongs-to,Ruby On Rails,Ruby,Postgresql,Has Many Through,Belongs To,我有一个关系模型,在这个模型中,两个用户可以为交换两个项目进行交易 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_n

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

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
堆栈跟踪指向我用来列出所有交易请求的助手方法。该行显示
@trades=current\u user.trades\u received.requested.count
,然后向下到用户上的模型关联,其中
有很多:拥有的项目,类名称:“项目”
。根据我的理解,它看起来像是
trades\u received
方法,它被称为
through::owned\u items
source::trades
应该在迁移中引用
:wanted\u item\u id
外键。但事实并非如此。如果我创建一个迁移来添加
item\u id
,它会起作用,但交易需要两个项目,因此我将其拆分为两个
想要的项目
抵押品项目
关联。如何设置该用户关联,使其引用其他用户请求的项目?项目
是否应该像我这样拥有多个:交易
,或者项目
是否应该属于:交易

完全错误:

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" = $1 AND "trades"."approved" IS NULL

tldr:我需要跟踪大量复杂的
关联:通过
关联,我认为我的数据模型不正确,需要帮助理解原因。谢谢。

好的。问题在于:

has_many :trades, dependent: :destroy
在您的
交易
模型中:

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
class用户
如果您需要将所有交易作为单一关联:


嗯,你会有麻烦:)在这种情况下,你应该手动选择关联,或者重新考虑你的数据模型。

你正在设置两个
有很多:通过
用户
项目
之间的关系,将
交易
作为两者的联接表。你把关系搞糊涂了。以下是基于您的迁移的设置:

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

您所说的“手动选择关联,或重新考虑您的数据模型”是什么意思?
:通缉交易
:收到的交易
不是单独的关联吗?另外,为什么在项目模型中有两次
:通缉交易
?这难道不是两次引用同一个项目吗?@sabaeus抱歉,我不小心放弃了编辑。更新了我的答案。商品不能只
有许多交易
,因为交易与商品之间没有直接的联系(
商品id
或其他任何东西)。因此,你必须将关联分开。如果您想为项目选择所有交易,您必须手动选择它们(
Trade.where(“通缉的\u项目\u id=?或抵押品\u项目\u id=?”,self.id
)或者改变你的数据库模式。哦,那么,
inverse\u of
创建了这个链接?哇,这终于有意义了。谢谢你!不过我对另一件事感到困惑。你是说手动选择是不明智的,还是可能是一种痛苦?我只是问,因为我最初的帖子有多个交易关联,但你的回答是“如果你需要将所有交易作为一个单一的关联”这几乎总是一件痛苦的事。当你在功能中需要添加一些额外的逻辑-过滤器、要查看的新数据,无论什么-这样做将是一件痛苦的事。@sabaeus抱歉,但我不能帮你:(
User有很多:trades\u received
。所以它应该是
current\u User。trades\u received
。可能最好将其更改为
received\u trades
。您的
\requested
方法是什么样的?哪一行给出了错误?
requested
是一个范围,它只是
范围:requested,->{(核准:无)
。我的错误来自于我在标题中使用的一个帮助程序,并向下延伸到用户模型的
:trades\u received
关联。@unkmas建议这是因为我的设置在Trade和Item之间没有直接链接。@sabaeus你能粘贴数据库架构吗file@sabaeus将此t.U添加到:迁移贸易。@krishnar将其添加到交易迁移中?谢谢,这是有意义的。但问题是:我认为外键只应该位于关系的
属于
一侧?这两个
都有许多
属于
都有
外键
选项。当外键不遵循ActiveRecord co时使用它nvention(即关联名+“_id”),这是
在这里有很多关系的情况。但是为什么我要把它放在一边而不是另一边呢?当Rails无法推断出正确的外键时,你需要使用它。比如说,如果你可以有类似
属于:user,foreign\u key:“trade\u requester”这样的东西“
i n
Trade
而不是我们现有的。没有一种方法可以做到这一点。不知道你的意思。Trade有两个属于具有正确外键的用户。问题是什么?
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.