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?
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 :kategoriI think you meant :category.> end > > class Category < ActiveRecord::Base > has_many :products, :through =>:categorizations > has_many :categorizations > endThis 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 tableIt 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 marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://www.ruby-forum.com/.
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 marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org> > I hope that answers your question! > > > Best, > -- > Marnen Laibow-Koser > http://www.marnen.org > marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org-- Posted via http://www.ruby-forum.com/.
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- s.net> wrote:> 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-Koserhttp://www.marnen.org > mar...-sbuyVjPbboAdnm+yROfE0A@public.gmane.org > > > > > I hope that answers your question! > > > Best, > > -- > > Marnen Laibow-Koser > >http://www.marnen.org > > mar...-sbuyVjPbboAdnm+yROfE0A@public.gmane.org > > -- > Posted viahttp://www.ruby-forum.com/.
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 marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://www.ruby-forum.com/.
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- s.net> wrote:> 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-Koserhttp://www.marnen.org > mar...-sbuyVjPbboAdnm+yROfE0A@public.gmane.org > -- > Posted viahttp://www.ruby-forum.com/.
Seemingly Similar Threads
- uniqueness validation perplexity
- validates preference of at least one param
- Authlogic Password confirmation is too short Error. NEED HELP.
- <b> tag in HTML 5 (was Re: Re: assert_select for <p><b>text</b>value</p>)
- acts_as_list / acts_as_tree / acts_as_nested_set - which one