Thursday, November 29, 2012

[Rails] Re: has_and_belongs_to_many for the same model

>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 via http://www.ruby-forum.com/.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
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.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate