Forum: Ruby on Rails adding habtm through migrations

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.
1b0da6ef1b9583bc2bc926822a98e088?d=identicon&s=25 Srdjan Marinovic (Guest)
on 2006-06-05 10:56
(Received via mailing list)
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
B780ee0ee1480454a85df58536702f63?d=identicon&s=25 Alder Green (Guest)
on 2006-06-05 11:14
(Received via mailing list)
On 6/5/06, Srdjan Marinovic <srdjan.marinovic@gmail.com> 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.
1b0da6ef1b9583bc2bc926822a98e088?d=identicon&s=25 Srdjan Marinovic (Guest)
on 2006-06-05 12:31
(Received via mailing list)
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
9f0f89bbd9e1ecfbaab6584e429b7a2f?d=identicon&s=25 Josh Susser (jsusser)
on 2006-06-05 17:04
Srdjan Marinovic 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...

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...

--
Josh Susser
http://blog.hasmanythrough.com
1b0da6ef1b9583bc2bc926822a98e088?d=identicon&s=25 Srdjan Marinovic (Guest)
on 2006-06-05 17:52
(Received via mailing list)
>
> add_index(:categories_products, [:category_id, :product_id], :unique =>
> true)
>
> 
http://rails.outertrack.com/module/ActiveRecord%3A...
>
> --
> Josh Susser
> http://blog.hasmanythrough.com

cheers mate, it works perfect now

srdjan
Addba6649b49343601c8e836d8fa5c86?d=identicon&s=25 Thomas Townsend (Guest)
on 2006-06-08 17:09
(Received via mailing list)
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
This topic is locked and can not be replied to.