Forum: Ruby on Rails has_and_belongs_to_many for the same model

Posted by Soichi Ishida (soichi)
on 2012-11-29 04:33
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
Posted by Greg Donald (destiney)
on 2012-11-29 04:38
(Received via mailing list)
On Wed, Nov 28, 2012 at 9:33 PM, Soichi Ishida <lists@ruby-forum.com> 
wrote:
> Somehow I want to find a model object from the same object.
> Do you think we can do that?

https://www.google.com/search?q=rails+self+referential



--
Greg Donald
Posted by Jordon Bedwell (Guest)
on 2012-11-29 09:12
(Received via mailing list)
On Wed, Nov 28, 2012 at 9:33 PM, Soichi Ishida <lists@ruby-forum.com> 
wrote:
> I am wondering if has_and_belongs_to_many association can be used for a
> single model?

Circular associations are possible, I do it all the time for comment 
systems.
Posted by Soichi Ishida (soichi)
on 2012-11-29 09:29
>https://www.google.com/search?q=rails+self+referential

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
Posted by Jim ruther Nill (jimboker)
on 2012-11-29 09:44
(Received via mailing list)
On Thu, Nov 29, 2012 at 4:29 PM, Soichi Ishida <lists@ruby-forum.com> 
wrote:

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


> To post to this group, send email to rubyonrails-talk@googlegroups.com.
> To unsubscribe from this group, send email to
> rubyonrails-talk+unsubscribe@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>
>


--
Posted by Soichi Ishida (soichi)
on 2012-11-29 09:55
>i don't see any give_plan on your code.

Sorry, I pasted a wrong portion of code error.

it's

> undefined method `give_person' for []:ActiveRecord::Relation
> undefined method `give_person' for nil:NilClass
Posted by Soichi Ishida (soichi)
on 2012-11-29 10:02
>>
>> 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
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.