# Has\_and\_belongs\_to\_many between the same model

**URL:** <https://www.ruby-forum.com/t/has-and-belongs-to-many-between-the-same-model/120086>\
**Category:** Rails\
**Created:** [November 13, 2007, 5:07pm UTC](https://www.ruby-forum.com/t/has-and-belongs-to-many-between-the-same-model/120086 "2007-11-13T17:07:14Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![albertosesena](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f9ae1b/32.png) [@albertosesena](https://www.ruby-forum.com/u/albertosesena)\
**Post date:** [November 13, 2007, 5:07pm UTC](https://www.ruby-forum.com/t/has-and-belongs-to-many-between-the-same-model/120086/1 "2007-11-13T17:07:14Z")

</div>

Hi! I have two models: Jewel and Crate. A crate can contain other crates  
and it can also contain as many jewels as we want. So, in jewel.rb i  
put:

```auto
  belongs_to :crate
```

and in crate.rb:

```auto
  has_many :jewels
  has_and_belongs_to_many :crates
```

Now i have to create the join table “crates\_crates” but i really have no  
idea on how to specify the first index is the father-crade index while  
the second is the child-crade index. Btw, my “crades” table has a  
“crade\_id” column, is this right, or should i remove it since i use a  
join table?

Thanks!

---

<div class="post-metadata">

**Author:** ![albertosesena](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f9ae1b/32.png) [@albertosesena](https://www.ruby-forum.com/u/albertosesena)\
**Post date:** [November 13, 2007, 5:15pm UTC](https://www.ruby-forum.com/t/has-and-belongs-to-many-between-the-same-model/120086/2 "2007-11-13T17:15:16Z")

</div>

as far as i can see, you don’t need the extra table, since crate has in  
fact only a has\_many (children) plus a belongs\_to (…only one parent)  
relationship with itself (it can have several other crates, but can  
belong only to one ‘parent-crate’)  
so keep the crate\_id and kick the crates\_crates table

if i got it wrong and you really need crates\_crates, then you can omit  
crate\_id

---

<div class="post-metadata">

**Author:** ![albertosesena](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f9ae1b/32.png) [@albertosesena](https://www.ruby-forum.com/u/albertosesena)\
**Post date:** [November 13, 2007, 5:18pm UTC](https://www.ruby-forum.com/t/has-and-belongs-to-many-between-the-same-model/120086/3 "2007-11-13T17:18:40Z")

</div>

So, you suggest avoiding habtm and using something like:

crate.rb

```auto
  has_many :jewels, :crates
  belongs_to :crate
```

if i got it right 😉

---

<div class="post-metadata">

**Author:** ![albertosesena](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f9ae1b/32.png) [@albertosesena](https://www.ruby-forum.com/u/albertosesena)\
**Post date:** [November 13, 2007, 5:24pm UTC](https://www.ruby-forum.com/t/has-and-belongs-to-many-between-the-same-model/120086/4 "2007-11-13T17:24:30Z")

</div>

Yes, you’re right… that was just an unuseful complication. Thanks a  
lot!

---

<div class="post-metadata">

**Author:** ![albertosesena](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f9ae1b/32.png) [@albertosesena](https://www.ruby-forum.com/u/albertosesena)\
**Post date:** [November 13, 2007, 5:21pm UTC](https://www.ruby-forum.com/t/has-and-belongs-to-many-between-the-same-model/120086/5 "2007-11-13T17:21:28Z")

</div>

yep, right. keep as simple as possible.  
as long as it can do the thing you want.
