# Has\_many through , or habtm , using form

**URL:** <https://www.ruby-forum.com/t/has-many-through-or-habtm-using-form/170762>\
**Category:** Rails\
**Created:** [June 22, 2009, 3:33pm UTC](https://www.ruby-forum.com/t/has-many-through-or-habtm-using-form/170762 "2009-06-22T15:33:14Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![lecielbleu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/b487fb/32.png) [@lecielbleu](https://www.ruby-forum.com/u/lecielbleu)\
**Post date:** [June 22, 2009, 3:33pm UTC](https://www.ruby-forum.com/t/has-many-through-or-habtm-using-form/170762/1 "2009-06-22T15:33:14Z")

</div>

i think there ara two ways of relate products and categories ,  
basically i want to fix one product(e.g hp dv7…) to some categories  
(notebook,17"notebooks…)  
i made a table named categorization(incuding category\_id,product\_id  
fields) then in models i write these codes below

class Product \< ActiveRecord::Base  
has\_many :categories, :through =\> :categorizations  
has\_many :categorizations  
end

class Categorization \< ActiveRecord::Base  
belongs\_to :product  
belongs\_to :kategori  
end

class Category \< ActiveRecord::Base  
has\_many :products, :through =\>:categorizations  
has\_many :categorizations  
end

i’m not sure how rails complain which field will be use for relation  
products and categories in categorization table(this is missing chain  
i think)…anyway if we continue 🙂 , i have to make form (creating  
new product and add a new row in categorization table for identify  
categories of product we want to add at the same time )

which method should i use for add new product and its categories at  
the same time?

---

<div class="post-metadata">

**Author:** ![lecielbleu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/b487fb/32.png) [@lecielbleu](https://www.ruby-forum.com/u/lecielbleu)\
**Post date:** [June 22, 2009, 5:48pm UTC](https://www.ruby-forum.com/t/has-many-through-or-habtm-using-form/170762/2 "2009-06-22T17:48:24Z")

</div>

lecielbleu wrote:

> i think there ara two ways of relate products and categories ,  
> basically i want to fix one product(e.g hp dv7…) to some categories  
> (notebook,17"notebooks…)  
> i made a table named categorization(incuding category\_id,product\_id  
> fields) then in models i write these codes below
> 
> class Product \< ActiveRecord::Base  
> has\_many :categories, :through =\> :categorizations  
> has\_many :categorizations  
> end
> 
> class Categorization \< ActiveRecord::Base  
> belongs\_to :product  
> belongs\_to :kategori

I think you meant :category.

> end
> 
> class Category \< ActiveRecord::Base  
> has\_many :products, :through =\>:categorizations  
> has\_many :categorizations  
> end

This all looks fine, but unless you’re planning to put additional data  
in the categorizations table, you might as well use habtm and write less  
code.

> i’m not sure how rails complain which field will be use for relation  
> products and categories in categorization table

It will expect product\_id and category\_id fields, just as with any other  
association.

> (this is missing chain  
> i think)…anyway if we continue 🙂 , i have to make form (creating  
> new product and add a new row in categorization table for identify  
> categories of product we want to add at the same time )
> 
> which method should i use for add new product and its categories at  
> the same time?

You’ll need to create the product and/or categories if they don’t  
already exist, then create a categorization to bind them together.  
Something like this:

product = Product.new(params[:product])  
category = Category.find(params[:category\_id])  
Categorization.create!(:product =\> product, :category =\> category)

I hope that answers your question!

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![lecielbleu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/b487fb/32.png) [@lecielbleu](https://www.ruby-forum.com/u/lecielbleu)\
**Post date:** [June 22, 2009, 5:57pm UTC](https://www.ruby-forum.com/t/has-many-through-or-habtm-using-form/170762/3 "2009-06-22T17:57:11Z")

</div>

Marnen Laibow-Koser wrote:  
[…]

> Something like this:
> 
> product = Product.new(params[:product])  
> category = Category.find(params[:category\_id])  
> Categorization.create!(:product =\> product, :category =\> category)

Silly me! Instead of that last line, you could do product.category\_ids  
\<\< category.id .

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

> I hope that answers your question!
> 
> ## Best,
> 
> Marnen Laibow-Koser  
> [http://www.marnen.org](http://www.marnen.org)  
> removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![lecielbleu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/b487fb/32.png) [@lecielbleu](https://www.ruby-forum.com/u/lecielbleu)\
**Post date:** [June 23, 2009, 12:45am UTC](https://www.ruby-forum.com/t/has-many-through-or-habtm-using-form/170762/4 "2009-06-23T00:45:35Z")

</div>

lecielbleu wrote:

> Many thanks , whats your suggestion about making a form using these  
> codes?

You can certainly do that. Check out Stephen Chu’s params[:fu] series  
of blog posts, as well as the Railscasts on complex forms and the  
documentation regarding nested resources in Rails 2.3.2.

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![lecielbleu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/b487fb/32.png) [@lecielbleu](https://www.ruby-forum.com/u/lecielbleu)\
**Post date:** [June 24, 2009, 4:32pm UTC](https://www.ruby-forum.com/t/has-many-through-or-habtm-using-form/170762/5 "2009-06-24T16:32:16Z")

</div>

Thanks Marnen i solved using Stephen Chu’s params[:fu] fantastic  
series 🙂

On Jun 23, 1:45 am, Marnen Laibow-Koser \<rails-mailing-l…@andreas-

---

<div class="post-metadata">

**Author:** ![lecielbleu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/b487fb/32.png) [@lecielbleu](https://www.ruby-forum.com/u/lecielbleu)\
**Post date:** [June 23, 2009, 12:23am UTC](https://www.ruby-forum.com/t/has-many-through-or-habtm-using-form/170762/6 "2009-06-23T00:23:26Z")

</div>

Many thanks , whats your suggestion about making a form using these  
codes?

On Jun 22, 6:57 pm, Marnen Laibow-Koser \<rails-mailing-l…@andreas-
