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


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

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

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

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”

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

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

Hey Anshu,

Taking a look at the code you posted, I don’t know for sure why it
would be repeating every so often, but you could try adding a unique
index on the categories table to force the database to “break” when a
duplicate is inserted. Obviously you might not want to do this with
production code, but if you can duplicate it in a test environment,
making it break would likely give you a stack trace you could use to
pinpoint the problem.

You could also add a check for uniqueness on the model:

class Category < ActiveRecord::Base
validates :name, :unique => true, :presence => true # Rails 3

Rails 2.x

validates_uniqueness_of :name


Another possibility that occurs to me is that MAYBE character codes
being inserted aren’t quite uniform. For example, if you have a
browser set to send information using UTF-8, and I have a browser set
to send POST data as ASCII, there might be similar symbols with
different character codes that the database or the Ruby interpreter is
looking at saying, “these are different” even though they’re the same
symbol. This is a remote possibility and probably not quite 100%
correct, and I’m sure some one can offer more information on this
point than I, as I don’t really deal with “foreign” character sets
that often, but it may be something to look into.

Nonetheless, if you specify the encoding type in your database for all
tables (UTF-8 would probably be the way to go depending on your needs)
and a unique index on the columns that repeat, you can probably get
it to break and show you some information that may help you track it

Another possibility to help figure it out would be writing some quick
unit tests to validate that information isn’t being repeated, then
running those tests. If they pass (and certify that information isn’t
being repeated), I’d think it more likely that something like the
character encoding issue I mentioned above might be going on.

I wish I could be of more assistance. Good luck to you!

Hi Phoenix R.,

Thanks for looking at my issue. I have UTF encoding for my databases and
for the rails app.

My hunch is that it got to do something with category table having
parent_id for acts_as_tree plugin requirement. Every time, I add a
children of root category
the root category gets added itself. It then repeats. This is surprising
and I have never experienced like this before.

As a temp work around I have added
has_many :products, :through => :categorizations, :uniq => true
but it won’t solve the problem root category getting inserted themselves
but should avoid multiple insertions. Note category 45 was assigned but
after some time parents adds itself repeatedly.

Here is sample of insertion
#<Category id: 45, parent_id: 12, name: “Books”, description: “”,
created_at: “2011-02-21 08:08:17”, updated_at: “2011-02-21 08:08:17”,
permalink: “books”>, #<Category id: 12, parent_id: 11, name: “Kids”,
description: “”, created_at: “2010-09-16 23:28:06”, updated_at:
“2011-01-05 11:17:10”, permalink: “kids”>, #<Category id: 12, parent_id:
11, name: “Kids”, description: “”, created_at: “2010-09-16 23:28:06”,
updated_at: “2011-01-05 11:17:10”, permalink: “kids”>, #<Category id:
12, parent_id: 11, name: “Kids”, description: “”, created_at:
“2010-09-16 23:28:06”, updated_at: “2011-01-05 11:17:10”, permalink:
“kids”>, #<Category id: 12, parent_id: 11, name: “Kids”, description: “”