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 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
On Wed, Nov 28, 2012 at 9:33 PM, Soichi Ishida <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> 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 -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
On Wed, Nov 28, 2012 at 9:33 PM, Soichi Ishida <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> 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. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
>https://www.google.com/search?q=rails+self+referentialThanks. 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
On Thu, Nov 29, 2012 at 4:29 PM, Soichi Ishida <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> >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 >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.> 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit https://groups.google.com/groups/opt_out. > > >-- ------------------------------------------------------------- visit my blog at http://jimlabs.heroku.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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
>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 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
>> >> 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 -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.