# Counter cache help

**URL:** <https://www.ruby-forum.com/t/counter-cache-help/175318>\
**Category:** Rails\
**Created:** [September 11, 2009, 5:52am UTC](https://www.ruby-forum.com/t/counter-cache-help/175318 "2009-09-11T05:52:42Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![chero](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e99b99/32.png) [@chero](https://www.ruby-forum.com/u/chero)\
**Post date:** [September 11, 2009, 5:52am UTC](https://www.ruby-forum.com/t/counter-cache-help/175318/1 "2009-09-11T05:52:42Z")

</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 = ?”, 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:** ![chero](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e99b99/32.png) [@chero](https://www.ruby-forum.com/u/chero)\
**Post date:** [September 11, 2009, 9:57am UTC](https://www.ruby-forum.com/t/counter-cache-help/175318/2 "2009-09-11T09:57:58Z")

</div>

Singularize your model names: Comment & TestCase

-e

---

<div class="post-metadata">

**Author:** ![chero](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e99b99/32.png) [@chero](https://www.ruby-forum.com/u/chero)\
**Post date:** [September 11, 2009, 1:42pm UTC](https://www.ruby-forum.com/t/counter-cache-help/175318/3 "2009-09-11T13:42:46Z")

</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 = ?”, user\_id] } }  
end

Sorry for the confusion,  
JIm

---

<div class="post-metadata">

**Author:** ![chero](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e99b99/32.png) [@chero](https://www.ruby-forum.com/u/chero)\
**Post date:** [September 11, 2009, 4:26pm UTC](https://www.ruby-forum.com/t/counter-cache-help/175318/4 "2009-09-11T16:26:42Z")

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