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
end

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
down.

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!


On Feb 22, 11:48 pm, Anshu Agr <[email protected]> wrote:
> 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 viahttp://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 [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to