# Counter cache help

**URL:** <https://discuss.rubyonrails.org/t/counter-cache-help/45963>\
**Category:** rubyonrails-talk\
**Created:** [September 11, 2009, 3:50am UTC](https://discuss.rubyonrails.org/t/counter-cache-help/45963 "2009-09-11T03:50:59Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![James\_Englert](https://avatars.discourse-cdn.com/v4/letter/j/bc79bd/32.png) [@James\_Englert](https://discuss.rubyonrails.org/u/James_Englert)\
**Post date:** [September 11, 2009, 3:50am UTC](https://discuss.rubyonrails.org/t/counter-cache-help/45963/1 "2009-09-11T03:50:59Z")

</div>

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](http://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?

---

<div class="post-metadata">

**Author:** ![Eric9](https://avatars.discourse-cdn.com/v4/letter/e/ac91a4/32.png) [@Eric9](https://discuss.rubyonrails.org/u/Eric9)\
**Post date:** [September 11, 2009, 7:12am UTC](https://discuss.rubyonrails.org/t/counter-cache-help/45963/2 "2009-09-11T07:12:35Z")

</div>

Singularize your model names: Comment & TestCase

-e

---

<div class="post-metadata">

**Author:** ![James\_Englert](https://avatars.discourse-cdn.com/v4/letter/j/bc79bd/32.png) [@James\_Englert](https://discuss.rubyonrails.org/u/James_Englert)\
**Post date:** [September 11, 2009, 11:40am UTC](https://discuss.rubyonrails.org/t/counter-cache-help/45963/3 "2009-09-11T11:40:38Z")

</div>

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](http://users.id) = ?”, user\_id ] } } end

Sorry for the confusion, JIm

---

<div class="post-metadata">

**Author:** ![James\_Englert](https://avatars.discourse-cdn.com/v4/letter/j/bc79bd/32.png) [@James\_Englert](https://discuss.rubyonrails.org/u/James_Englert)\
**Post date:** [September 11, 2009, 2:24pm UTC](https://discuss.rubyonrails.org/t/counter-cache-help/45963/4 "2009-09-11T14:24:50Z")

</div>

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