11155
(-- --)
November 29, 2012, 3:33am
1
Rails 3.1.3
I am wondering if has_and_belongs_to_many association can be used for a
single model?
More specifically, say I have a model called, Person, another model
called Meeting.
Person 1 and Person 2 share some kind of association through Meeting so
that
person1 = Person.find(...)
then,
person1.meeting returns person2.
If my understanding is correct, has_and_belongs_to_many association is
used for one model with another.
Somehow I want to find a model object from the same object.
Do you think we can do that?
soichi
Circular associations are possible, I do it all the time for comment systems.
11155
(-- --)
November 29, 2012, 8:29am
4
rails self referential - Google Search
Thanks. This is what I wanted.
But in my case, two model objects (in the RailsCasts, User) are equal.
So, I tried
in person.rb
has_many :give_meetinges, :class_name => "Meeting", :foreign_key =>
"give_id"
has_many :gives, :class_name => "Person", :through =>
:give_meetinges, :source => :person
has_many :take_meetinges, :class_name => "Meeting", :foreign_key =>
"take_id"
has_many :takes, :class_name => "Person", :through =>
:take_meetinges, :source => :person
in meeting.rb
belongs_to :give_person, :class_name => "Person"
belongs_to :take_person, :class_name => "Person"
in the view, ('take' is person object)
<%= take.give_meetings.give_person %>
raises an error,
undefined method `give_plan' for :ActiveRecord::Relation
Since it's 'Relation',
<%= take.give_meetings.first.give_person %>
also raises an error
undefined method `give_plan' for nil:NilClass
Can see problems in my code?
soichi
>rails self referential - Google Search
Thanks. This is what I wanted.
But in my case, two model objects (in the RailsCasts, User) are equal.
So, I tried
in person.rb
has_many :give_meetinges, :class_name => "Meeting", :foreign_key =>
"give_id"
has_many :gives, :class_name => "Person", :through =>
:give_meetinges, :source => :person
has_many :take_meetinges, :class_name => "Meeting", :foreign_key =>
"take_id"
has_many :takes, :class_name => "Person", :through =>
:take_meetinges, :source => :person
I'm not sure if you need to specify the class_name for a has_many through
association
in meeting.rb
belongs_to :give_person, :class_name => "Person"
belongs_to :take_person, :class_name => "Person"
this means that the foreign key is set to give_person_id and take_person_id
but in your person model, the foreign key is give_id and take_id
in the view, ('take' is person object)
<%= take.give_meetings.give_person %>
raises an error,
undefined method `give_plan' for :ActiveRecord::Relation
Since it's 'Relation',
<%= take.give_meetings.first.give_person %>
also raises an error
undefined method `give_plan' for nil:NilClass
this is an expected error if there are no give_meetings for take. if there
is at least on give_meetings for take, this will not raise an error
(assuming
that your associations and foreign keys are setup correctly)
i don't see any give_plan on your code. paste the code that stacktrace
points to the source of this error.
11155
(-- --)
November 29, 2012, 8:55am
6
i don't see any give_plan on your code.
Sorry, I pasted a wrong portion of code error.
it's
11155
(-- --)
November 29, 2012, 9:02am
7
in meeting.rb
belongs_to :give_person, :class_name => "Person"
belongs_to :take_person, :class_name => "Person"
this means that the foreign key is set to give_person_id and
take_person_id
but in your person model, the foreign key is give_id and take_id
This was the problem.
I changed them to give_person_id and take_person_id and now it works!
Thanks for your help!
soichi