0

我有三个模型:出于提问的目的,我将它们称为posts、containertable1 和containertable2。

基本上,posts 表有一个名为 misc_id 的列,其中包含 containertable1 或 containertable2 中的项目的 id。我试图建立一个外键关联,但它似乎没有从我指定的列中获取。

所以这就是我的 post.rb 模型的样子:
class Post < ActiveRecord::Base
belongs_to :containertable1, :polymorphic => true, :foreign_key => "misc_id"
belongs_to :containertable2, :polymorphic => true, :foreign_key => " misc_id"
set_table_name "事务"

然后我的 containertable1.rb 模型:
class Containertable1 < ActiveRecord::Base
has_many :transactions

但是在脚本/控制台中,当我运行 Containertable1.find(:first).posts 时,它给了我这个错误:

未知列“posts.containertable1_id”

我希望它会尝试寻找“misc_id”列。有人可以深入了解我做错了什么吗?非常感谢 :)

4

1 回答 1

0

我一直在玩这个一些,想我想通了。

外键实际上需要在 containertable.rb 模型中。所以这就是我改变的:

类 Containertable1 < ActiveRecord::Base has_many :posts, :foreign_key => "misc_id"

于 2010-01-15T01:31:03.773 回答