Hello all.
I am trying to create a self-referential has_many :through. I used the
following site as a guide
http://blog.hasmanythrough.com/articles/2006/04/21/self-referential-through
but it still doesn’t appear to be working. I have two models. Person and
Relationship. A person has many contacts (Which is another person)
through relationships
class Person < ActiveRecord::Base
has_many :relationship
has_many :contacts, :through => :relationship end
end
class Relationship < ActiveRecord::Base
belongs_to :person, :foreign_key => “person_id”
belongs_to :contact, :foreign_key => “contact_id”, :class_name =>
“Person”
end
If I use the code as above then while the relationships appear in the
view the only “Contact” that appears is the Person I am currently
looking at (So the Person “Person A” lists “Person A” as a contact).
If, on the other hand I do the following:
class Person < ActiveRecord::Base
has_many :relationship
has_many :contacts, :through => :relationship end
end
class Relationship < ActiveRecord::Base
belongs_to :person, :foreign_key => “person_id”
belongs_to :contact, :foreign_key => “contact_id”
end
class Contact < ActiveRecord::Base
set_table_name ‘people’
end
It works fine. It appears that setting the :contact to :class_name =>
“Person” screws up the relationship. I would rather avoid defining a
separate “Contact” class and use self-referential but I cannot seem to
do it.
Can anyone offer some advice?
Thanks
RJ