views:

17

answers:

1

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 "transactions"

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 :)

A: 

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"

Jess