Tuesday, February 22, 2011

[Rails] Rails Acts_as_tree and many to many associations (Product, Category) Issue

Hi,

I want to create simple many to many association between my product and
category model. Category model currently also acts_as_tree for
navigation. The code works fine but not sure why after updating product
categories, parent category is also inserted repeatedly. This is
surprising because new record in my join model is repeating with parent
category every few minutes. here are my model code

class Category < ActiveRecord::Base
acts_as_tree :order => 'name'

has_many :categorizations, :dependent => :destroy
has_many :products, :through => :categorizations
end

class Product < ActiveRecord::Base
has_many :categorizations, :dependent => :destroy
has_many :categories, :through => :categorizations
end

class Categorization < ActiveRecord::Base
belongs_to :product
belongs_to :category
end

I'm also pasting relevant fields from the schema to give more contexts
create_table "categories", :force => true do |t|
t.integer "parent_id"
t.string "name"
t.datetime "created_at"
t.datetime "updated_at"
end

create_table "categorizations", :force => true do |t|
t.integer "product_id"
t.integer "category_id"
t.datetime "created_at"
t.datetime "updated_at"
end

Any help is appreciated. Just to reiterate category assignment works but
after some times, parent category of the assigned category is inserted
repeatedly.

--
Posted via http://www.ruby-forum.com/.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate