Counter cache help

My :counter_cache column isn’t working properly.

I wouldn’t normally ask about a relatively trivial issue like this but I’ve been banging my head on this for a bit and haven’t gotten anywhere. I must be missing something simple. Please take a look:

class Comments < ActiveRecord::Base belongs_to :test_case, :counter_cache => true end

class TestCases < ActiveRecord::Base has_many :test_case_to_users has_many :comments has_many :users, :through => :test_case_to_users

named_scope :broken, :conditions => [ “broken = ?”, true ] named_scope :fixed, :conditions => [ “broken = ?”, false ] named_scope :for_user, lambda { |user_id| { :include => :users, :conditions => [“users.id = ?”, user_id ] } } end

class CreateComments < ActiveRecord::Migration def self.up create_table :comments do |t| t.string :name t.string :body

  t.integer :test_case_id

  t.timestamps
end

end

def self.down drop_table :comments end end

class CreateTestCases < ActiveRecord::Migration def self.up create_table :test_cases do |t| t.string :name t.boolean :broken, :default => true

  t.integer :comments_count, :default => 0
 
  t.timestamps
end

end

def self.down drop_table :test_cases end end

I’m getting the following:

NoMethodError: undefined method find' for ActiveRecord::TestCase:Class from /Library/Ruby/Gems/1.8/gems/activerecord-2.3.2/lib/active_record/associations/belongs_to_association.rb:44:in find_target’ from /Library/Ruby/Gems/1.8/gems/activerecord-2.3.2/lib/active_record/associations/association_proxy.rb:240:in load_target' from /Library/Ruby/Gems/1.8/gems/activerecord-2.3.2/lib/active_record/associations/association_proxy.rb:112:in reload’ from /Library/Ruby/Gems/1.8/gems/activerecord-2.3.2/lib/active_record/associations.rb:1231:in test_case' from /Library/Ruby/Gems/1.8/gems/activerecord-2.3.2/lib/active_record/associations.rb:1010:in send’ from /Library/Ruby/Gems/1.8/gems/activerecord-2.3.2/lib/active_record/associations.rb:1010:in belongs_to_counter_cache_after_create_for_test_case' from /Users/jimenglert/.gem/ruby/1.8/gems/activesupport-2.3.2/lib/active_support/callbacks.rb:178:in send’ from /Users/jimenglert/.gem/ruby/1.8/gems/activesupport-2.3.2/lib/active_support/callbacks.rb:178:in `evaluate_method’

Any ideas?

Singularize your model names: Comment & TestCase

-e

Thats actually how I originally had it. I was playing around with pluralizing them as a last resort and forgot to change them back. I get the same error with:

class Comment < ActiveRecord::Base belongs_to :test_case, :counter_cache => true end

class TestCase < ActiveRecord::Base has_many :test_case_to_users has_many :comments has_many :users, :through => :test_case_to_users

named_scope :broken, :conditions => [ “broken = ?”, true ] named_scope :fixed, :conditions => [ “broken = ?”, false ] named_scope :for_user, lambda { |user_id| { :include => :users, :conditions => [“users.id = ?”, user_id ] } } end

Sorry for the confusion, JIm

Figured it out. TestCase is also defined by active record. It will probably be easiest to just rename it from TestCase to something else.