开发者

:foreign_key not requesting from the right column?

开发者 https://www.devze.com 2022-12-16 16:51 出处:网络
I have three models:for the purpose of asking the question, I\'ll call them posts, containertable1 and containertable2.

I have three models: for the purpose of asking the question, I'll call them posts, containertable1 and containertable2.

Basically, the posts table has a column called misc_id, which has the id of an item that is in either containertable1 or containertable2. I tried to set up a foreign key association, but it doesn't seem to fetch from the column I specify.

So here's what my post.rb model looks like:

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 "transact开发者_如何学Cions"

And then my containertable1.rb model:

class Containertable1 < ActiveRecord::Base

has_many :transactions

But in script/console, when I run Containertable1.find(:first).posts, it gives me this error:

Unknown column 'posts.containertable1_id'

I was hoping that it'd try to look for the "misc_id" column instead. Can someone provide insight into what I'm doing wrong? Big thanks :)


I've been playing around with this some more, think I figured it out.

the foreign key actually needs to be in the containertable.rb models. So here's what I changed:

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

0

精彩评论

暂无评论...
验证码 换一张
取 消