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!
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 O. 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!
Aryk G. 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
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 O. [email protected]