One association from two

I’m working on a two-to-many relationship that works fine from the
child object’s perspective but not from the parent’s. The
imperfect, yet functional code looks like this:

class PrimaryTable < ActiveRecord::Base
has_many :secondary_tables_1 ,
:foreign_key => :A_primary_table_id,
:class_name => “SecondaryTable”
has_many :secondary_tables_2 ,
:foreign_key => :B_primary_table_id,
:class_name => “SecondaryTable”
end

class SecondaryTable < ActiveRecord::Base
belongs_to :A_primary_table,
:foreign_key => :A_primary_table_id,
:class_name => “PrimaryTable”
belongs_to :B_primary_table,
:foreign_key=> :B_primary_table_id,
:class_name => “PrimaryTable”
end

But, I don’t want to have two linking fields up and two linking
fields down. I only want one linking field down. I would like to
lump these parent to child relationships into one associated link…
doing something like this:


class PrimaryTable < ActiveRecord::Base
has_many :secondary_tables ,
:foreign_key => :A_primary_table_id,
:foreign_key => :B_primary_table_id,
:class_name => “SecondaryTable”
end


…but only one of the two links (the ones to “B_primary_table”) shows
up this way.

Does any body have any idea how to do this? I would think this would
be an easy thing to do.

Dave

dschruth wrote:

       :class_name => "SecondaryTable"

       :foreign_key => :A_primary_table_id,
       :foreign_key => :B_primary_table_id,
       :class_name => "SecondaryTable"

end


…but only one of the two links (the ones to “B_primary_table”) shows
up this way.

Using the :finder_sql option to has_many, you’ll have to write
custom SQL that does an “or” on the two foreign keys.


Rails Wheels - Find Plugins, List & Sell Plugins -
http://railswheels.com