Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

counter_cache with 'wrong' column name

#1680

Problems with :counter_cache

I found this 'problem' (I think) when was having two self relation on the table with the same table, for instance:

Having a model like that:


create_table :codes do |t|
  t.string :code_key, :null => false, :limit => 25
  t.string :file_name, :null => false, :limit => 200, :default => ""
  t.integer :language_id, :null => false, :default => 10
  t.text :code, :null => false
  t.string :comment, :limit => 250, :default => ""
  t.boolean :private, :null => false, :default => false
  t.integer :reply_id
  t.integer :reply_id_count, :integer, :default => 0, :null => false
  t.timestamps
end

In the class:


# this is a self rellation....
belongs_to :reply, :class_name => "Code", :foreign_key => "reply_id", :counter_cache => true

I'm willing to have with the self rellation the counter for that. But a problem occours when I'm try to make an insert:


SQLite3::SQLException: no such column: codes_count: UPDATE "codes" SET "codes_count" = COALESCE("codes_count", 0) + 1 WHERE ("id" = 1)

I was surprised that the rails is trying to update a column called: 'codes_count', but I don't have a column with this name! It is using the :class_name as a prefix of the collumn _count.

Joshua, I really dont want to create a new job for you, at the very first day of the year, so I already get the rails code from github, but I'm getting some problems to understand the structure and code, if you could help me, I can fix it.

If OK for you to help me on that, tell me what is the best way to get in touch? IRC? MSN?

Regards!

Reported by Marcio Garcia · January 1st, 2009 @ 02:33 PM

State: invalid
Milestone: 2.x
Assigned to: Pratik Pratik
Importance: none

Activity

  1. josh
    josh
    • Assigned user changed from josh to Pratik

    Thanks for the report Marcio!

    But I don't think I'm the right person to handle AR tickets at the moment. I'll reassign it to Pratik for now, he has more knowledge in this area.

    January 2nd, 2009 @ 03:54 AM

  2. Pratik
    Pratik
    • State changed from new to invalid

    You should specify a custom column name :

    
    belongs_to :reply, :class_name => "Code", :foreign_key => "reply_id", :counter_cache => :reply_id_count
    

    If that doesn't work, it's a bug. And maybe you could submit a patch/test - http://guides.rails.info/contrib...

    Thanks :)

    March 13th, 2009 @ 02:11 PM

  3. bingbing