Adding habtm through migrations

Hello,

I’m stuck, I’ve got a model Product and would like to add a new model
called Category. Furthermore, I want to set up a many2many
relationship between the mentioned models. the problem is that I do
not know how to set the primary key in the categories_products table.

this is what I’ve got in my migration file:

create_table :categories do |t|
t.column :name, :string

Other fields here

end

create_table :categories_products do |t|
t.column :category_id, :integer
t.column :product_id, :integer
#how do I set the primary key to be (category_id, product_id)
end

Cheers

Regards

srdjan

On 6/5/06, Srdjan M. [email protected] wrote:

t.column :name, :string

Other fields here

end

create_table :categories_products do |t|
t.column :category_id, :integer
t.column :product_id, :integer
#how do I set the primary key to be (category_id, product_id)
end

From what you wrote, it doesn’t look like you need to. The HABTM
association should work through that table as it is.

hi,

From what you wrote, it doesn’t look like you need to. The HABTM
association should work through that table as it is.

-Alder

it does work, sort of. Whenever I reference a category through the
relationship I get it as a readonly object.
e.g.

p = Product.find :first
c = p.categories.first #c is readonly and it cannot be modified

This problem goes away when one sets the primary key as
primary key (category_id, product_id)
through SQL code.

I hoped that there would be a rails way to do the primary key setting.

cheers

srdjan

add_index(:categories_products, [:category_id, :product_id], :unique =>
true)

http://rails.outertrack.com/module/ActiveRecord%3A%3AConnectionAdapters%3A%3ASchemaStatements


Josh S.
http://blog.hasmanythrough.com

cheers mate, it works perfect now

srdjan

Srdjan M. wrote:

hi,

From what you wrote, it doesn’t look like you need to. The HABTM
association should work through that table as it is.

-Alder

it does work, sort of. Whenever I reference a category through the
relationship I get it as a readonly object.
e.g.

p = Product.find :first
c = p.categories.first #c is readonly and it cannot be modified

This problem goes away when one sets the primary key as
primary key (category_id, product_id)
through SQL code.

I hoped that there would be a rails way to do the primary key setting.

I think what’s going on is that your migration is adding an “id” field
as the primary key. That’s the default behavior of the create_table
method. Look at your table’s schema in an SQL tool to see what’s going
on there. To disable generation of an “id” field, add a “:id => false”
option to create_table.

http://rails.outertrack.com/module/ActiveRecord%3A%3AConnectionAdapters%3A%3ASchemaStatements/create_table

If for some reason you still need to set the primary key to the
combination of the foreign keys, the migration method is:

add_index(:categories_products, [:category_id, :product_id], :unique =>
true)

http://rails.outertrack.com/module/ActiveRecord%3A%3AConnectionAdapters%3A%3ASchemaStatements


Josh S.
http://blog.hasmanythrough.com

Here’s a super-newbie question… How do I go about adding child
records?

I have a table called ‘accounts’ to which I want to add invoices (an
account can have many invoices). I have the two models (accounts and
invoices) all set up.

On the account web page, I have a button_to link to an action called
‘add_invoice’. I added a def for ‘add_invoice’ in account.rb, but I
can’t figure out what to put inside the def. Is another controller
needed (invoice.rb)? Can I do it from within account.rb?

I’m trying to wrap my head around this concept - can anyone help?

TomT