Skip to content


Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP


reset_counters with namespaced models #4293

mulasse opened this Issue · 9 comments

7 participants

Florian Dutey Aaron Patterson Dan Pickett Arun Agrawal Isaac Sanders Richard Schneeman Carlos Antonio da Silva
Florian Dutey


I have the following issue:
When I try to use reset_counters on any of my associations with a counter_cache, i got the following error:

NoMethodError: undefined method `counter_cache_column' for nil:NilClass

I read documentation, and finally, had to open github, look at counter_cache.rb and test it in a rails console.

        child_class = has_many_association.klass
        belongs_to = child_class.reflect_on_all_associations(:belongs_to)
        reflection = belongs_to.find { |e| e.class_name == expected_name }
        counter_name = reflection.counter_cache_column

Error is on line 31 counter_name = because reflection is nil.
In my case, my models are namespaced (writing an engine).

Then, on line 30 reflection =, it can't find any association with expected_name.
Because expected_name is Formol::Topic and e.class_name is Topic

I fixed the error by specifying manually the class_name in belongs_to.
I don't know if class_name can be "guessed" in a better way with namespaces for reset_counters but it will be great.
It's a little bit shame to have to specify class_name on the association only to use reset_counters.

Best regards and happy new year.

Aaron Patterson

Can you add the code you're using for your models? I think I see what you're saying, but some test code that reproduces the problem would go a long way. Thanks.

Florian Dutey


errors issued when executing following code:

Formol::Topic.all.each do |t|
  #problem is the same with executing that line out of a 'each'
  Formol::Topic.reset_counters(, :posts)

I solved my problem by adding :class_name => 'Formol::Topic' on belongs_to :topic in post.rb (line 8)

Dan Pickett

I introduced an errant test here:

isn't this by design? I thought Rails assumes a root namespace for associations. Happy to dig in further for a potential fix if not.

Arun Agrawal

Closing this as #4370 is merged.

Arun Agrawal arunagw closed this
Dan Pickett

Please reopen I erroneously referenced an two unrelated issues #4370 does not resolve this issue

Arun Agrawal arunagw reopened this
Arun Agrawal

ok thanks for info.

Isaac Sanders

Is this still an issue? It seems like there was just a failure to use the right naming semantics...

Richard Schneeman

5 months no meaningful discussion, lets close this. If still a problem, add some tests or a fix in a new PR and reference this issue.

Carlos Antonio da Silva

Closing, if you still have any related issue, please let us know. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.