Forum: Ruby on Rails RE: HABTM - what am I doing wrong?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
HOGANBP (Guest)
on 2005-11-12 12:49
(Received via mailing list)
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 :)
charlesleeds (Guest)
on 2005-11-12 12:49
(Received via mailing list)
Thanks!  Now I need to figure out how to force migrate to not create me
an id field for items_subjects.
This topic is locked and can not be replied to.