# Has\_and\_belongs\_to\_many question

**URL:** <https://www.ruby-forum.com/t/has-and-belongs-to-many-question/72194>\
**Category:** Rails\
**Created:** [October 15, 2006, 11:13am UTC](https://www.ruby-forum.com/t/has-and-belongs-to-many-question/72194 "2006-10-15T11:13:16Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Petr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ac8455/32.png) [@Petr](https://www.ruby-forum.com/u/Petr)\
**Post date:** [October 15, 2006, 11:13am UTC](https://www.ruby-forum.com/t/has-and-belongs-to-many-question/72194/1 "2006-10-15T11:13:16Z")

</div>

Hi,  
ive got 3 tables Products, Categories and products\_categories. The  
relationship between products and categories tables is  
has\_and\_belongs\_to\_many. This is working fine. But I have to find out  
how to add and remove relationships to/from the products\_categories  
table.

For example, a new product is added and it needs to be put in a category  
of choice. Or a product is obsolete and is remove from its current  
category.

Can I get some help here?

Thanks

Petr

---

<div class="post-metadata">

**Author:** ![Petr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ac8455/32.png) [@Petr](https://www.ruby-forum.com/u/Petr)\
**Post date:** [October 15, 2006, 11:52am UTC](https://www.ruby-forum.com/t/has-and-belongs-to-many-question/72194/2 "2006-10-15T11:52:00Z")

</div>

Hi Petr,

I had the same problem yesterday and found this. It worked brilliantly  
for me.

[http://jrhicks.net/Projects/rails/has\_many\_and\_belongs\_to\_many.pdf](http://jrhicks.net/Projects/rails/has_many_and_belongs_to_many.pdf)

---

<div class="post-metadata">

**Author:** ![Petr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ac8455/32.png) [@Petr](https://www.ruby-forum.com/u/Petr)\
**Post date:** [October 15, 2006, 12:38pm UTC](https://www.ruby-forum.com/t/has-and-belongs-to-many-question/72194/3 "2006-10-15T12:38:39Z")

</div>

Hi and thanks,

But Im not sure how this will work for me. I just tried playing with a  
bit and unsuccessfully.

Let me give you a better example.

Theres a products table with products 1-100 in it. Theres a category  
table with categories 1-10 in it. Then theres products\_categores table  
that has columns product\_id and category\_id in it. It works fine if I  
manually (via SQL) add something like product\_id = 100 and category\_id =  
4. But How do I create or destroy such relationship in RoR and with  
has\_and\_belongs\_to\_many?

Petr

---

<div class="post-metadata">

**Author:** ![Petr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ac8455/32.png) [@Petr](https://www.ruby-forum.com/u/Petr)\
**Post date:** [October 15, 2006, 12:54pm UTC](https://www.ruby-forum.com/t/has-and-belongs-to-many-question/72194/4 "2006-10-15T12:54:19Z")

</div>

I made a progress. I can now add relationships with

if product.save  
category = Category.find(params[:objectid])  
product.categories \<\< category  
end

but how do I destroy them?

---

<div class="post-metadata">

**Author:** ![Petr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ac8455/32.png) [@Petr](https://www.ruby-forum.com/u/Petr)\
**Post date:** [October 15, 2006, 1:41pm UTC](https://www.ruby-forum.com/t/has-and-belongs-to-many-question/72194/5 "2006-10-15T13:41:29Z")

</div>

Cool!

Updating the Product.categories (which comes from the  
:has\_and\_belongs\_to\_many relationship) should do the trick (it does for  
me).

For example in your controller you have (in short):

def update\_product  
@product = Product.find(@params[:id])  
@product.categories = Category.find(@params[:category\_ids])  
@product.update\_attributes(@params[:product])  
end

For reading a few other things regarding this problem, the secret is in  
the [] of name=“category\_ids[]” (in the checkbox field as shown on the  
document for example).

So that should update your join table if you untick a checkbox for  
example.  
Also if you destroy a Product, then all references to the product\_id  
should disappear (I’ve tried it and it worked).

---

<div class="post-metadata">

**Author:** ![Petr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ac8455/32.png) [@Petr](https://www.ruby-forum.com/u/Petr)\
**Post date:** [October 15, 2006, 1:57pm UTC](https://www.ruby-forum.com/t/has-and-belongs-to-many-question/72194/6 "2006-10-15T13:57:51Z")

</div>

By the way, is your join table really called products\_categories?

If so then it’s not following Rails convention which is alphabetical  
order. I think you can use an option to the :has\_and\_belongs\_to\_many if  
you want to use a different name but I would recommend going the  
conventional way :).
