Has_and_belongs_to_many between the same model

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:

  belongs_to :crate

and in crate.rb:

  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!

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

So, you suggest avoiding habtm and using something like:

crate.rb

  has_many :jewels, :crates
  belongs_to :crate

if i got it right :wink:

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

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