RE: HABTM - what am I doing wrong?


#1

No, that’s expected behavior.

HABTM does not use a model table.

If I have

items
id
name
And

subjects
id
name

HABTM will expect a table that looks like this:

items_subjects

book_id
category_id

There is no ‘id’ field in this table because it shouldn’t need one.

You should be creating an item and then doing
@item << @subject

to add the subject.

You shouldn’t need a model for items_subjects

Also, unless you’re doing something more than what I think you’re doing,
you don’t need any of this:

Item.establish_connection(
:adapter => ‘mysql’,
:host => ‘localhost’,
:username => ‘root’,
:password => ‘password’,
:database => ‘retainit_test’
)

Subject.establish_connection(
:adapter => ‘mysql’,
:host => ‘localhost’,
:username => ‘root’,
:password => ‘password’,
:database => ‘retainit_test’
)

Items_Subjects.establish_connection(
:adapter => ‘mysql’,
:host => ‘localhost’,
:username => ‘root’,
:password => ‘password’,
:database => ‘retainit_test’
)

That’s why database.yml is for…

Hope that helps… Have you searched the WIKI for HABTM? Do you have the
Agile book?

Let me know if that’s clear as mud :slight_smile:


#2

Thanks! Now I need to figure out how to force migrate to not create me
an id field for items_subjects.