Is it posible to implement "Has and belongs to many" with a single table? Like this:>>table eventsid: integer description: text>>table related_eventsid_event: integer id_related_event: integer class Event < ActiveRecord::Base # How can i add a has and belong to many reference to self? end Thank you! -- 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
I would recommend doing a has_many :through back to itself. Just make sure you do call the event table two different things like: class RelatedEvents belongs_to :first_event belongs_to :second_event end Emmanuel Oga wrote:> Is it posible to implement "Has and belongs to many" with a single > table? > > Like this: > >>>table events > id: integer > description: text > >>>table related_events > id_event: integer > id_related_event: integer > > class Event < ActiveRecord::Base > > # How can i add a has and belong to many reference to self? > > end > > > Thank you!-- 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Aryk Grosz wrote:> I would recommend doing a has_many :through back to itself. Just make > sure you do call the event table two different things like: > > class RelatedEvents > belongs_to :first_event > belongs_to :second_event > end > > >Mmmmm i don''t quite get the idea :( -- 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Elad Meidar - Creopolis.com
2007-Jun-13 23:14 UTC
Re: Has and belongs to many with a single table
It''s pretty simple, all you need to do is to create 2 foreign keys and specify to where they point to. class Event << ActiveRecord::Base belongs_to :first_event, :class_name => "Event", :foreign_key => "first_event_id" belongs_to :second_event, :class_name => "Event", :foreign_key => "second_event_id" end On Jun 13, 3:33 pm, Emmanuel Oga <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Aryk Grosz wrote: > > I would recommend doing a has_many :through back to itself. Just make > > sure you do call the event table two different things like: > > > class RelatedEvents > > belongs_to :first_event > > belongs_to :second_event > > end > > Mmmmm i don''t quite get the idea :( > > -- > Posted viahttp://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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---