This project is archived and is in readonly mode.

#1680 ✓invalid
Marcio Garcia

counter_cache with 'wrong' column name

Reported by Marcio Garcia | January 1st, 2009 @ 02:33 PM | in 2.x

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

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?


Comments and changes to this ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href=""></a>

People watching this ticket