Has_many through , or habtm , using form

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 :slight_smile: , 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?

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 :slight_smile: , 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
[email protected]

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
[email protected]

I hope that answers your question!

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

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
[email protected]

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

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

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-