Dave S. wrote:
If I have a class, Asset, which has and belongs to many associated Asset
objects, how do I do this?
has_and_belongs_to_many :associated_assets, :class => “Asset”,
:foreign_key => ???, :association_foreign_key => ???
Right now, my table has a :first_asset_id and a :second_asset_id, but I
don’t see how the objects themselves are supposed to distinguish one
from the other.
You should be able to do this by using TWO associations and specifying a
slew of options. You need to override pretty much all the basic
defaults. Create a join table called ‘assets_assets’ for the below
has_and_belongs_to_many :first_assets, :class_name => “Asset”,
:join_table => “assets_assets”, :foreign_key => “second_asset_id”,
:association_foreign_key => “first_asset_id”
has_and_belongs_to_many :second_assets, :class_name => “Asset”,
:join_table => “assets_assets”, :foreign_key => “first_asset_id”,
:association_foreign_key => “second_asset_id”
Change those association names as you want. Note, this is written in the
email so it’s untested. It’s straightforward though so I’d expect it to