# Has and belongs to many with a single table

**URL:** <https://www.ruby-forum.com/t/has-and-belongs-to-many-with-a-single-table/106745>\
**Category:** Rails\
**Created:** [June 13, 2007, 7:39pm UTC](https://www.ruby-forum.com/t/has-and-belongs-to-many-with-a-single-table/106745 "2007-06-13T19:39:39Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![emmanueloga](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ecae2f/32.png) [@emmanueloga](https://www.ruby-forum.com/u/emmanueloga)\
**Post date:** [June 13, 2007, 7:39pm UTC](https://www.ruby-forum.com/t/has-and-belongs-to-many-with-a-single-table/106745/1 "2007-06-13T19:39:39Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![emmanueloga](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ecae2f/32.png) [@emmanueloga](https://www.ruby-forum.com/u/emmanueloga)\
**Post date:** [June 13, 2007, 8:17pm UTC](https://www.ruby-forum.com/t/has-and-belongs-to-many-with-a-single-table/106745/2 "2007-06-13T20:17:55Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![emmanueloga](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ecae2f/32.png) [@emmanueloga](https://www.ruby-forum.com/u/emmanueloga)\
**Post date:** [June 13, 2007, 9:33pm UTC](https://www.ruby-forum.com/t/has-and-belongs-to-many-with-a-single-table/106745/3 "2007-06-13T21:33:41Z")

</div>

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 ☹

---

<div class="post-metadata">

**Author:** ![emmanueloga](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ecae2f/32.png) [@emmanueloga](https://www.ruby-forum.com/u/emmanueloga)\
**Post date:** [June 14, 2007, 1:14am UTC](https://www.ruby-forum.com/t/has-and-belongs-to-many-with-a-single-table/106745/4 "2007-06-14T01:14:56Z")

</div>

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. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
