Skip to content

counter cache should be aware of self-referential belongs_to association #13137

Closed
wants to merge 1 commit into from

4 participants

@nurey
nurey commented Dec 2, 2013

counter cache is currently not working in rails 4-0-stable when using self-referential belongs_to association.
eg.

class Message
  belongs_to :parent_message, class_name: 'Message', foreign_key: 'parent_id', inverse_of: :replies, counter_cache: :replies_count
end
@dmitry
dmitry commented Dec 3, 2013

I guess tests should use at least existing models, but better associations too.

@senny
Ruby on Rails member
senny commented Dec 3, 2013

@nurey yes no new models please.

@nurey
nurey commented Dec 3, 2013

Which existing model should I use? Topic seems bloated and already has a replies association and a counter cache

@nurey
nurey commented Dec 3, 2013

Using the existing Reply model is not a good idea because it is a STI subclass of Topic so the old implementation of belongs_to_counter_cache_after_update_for_#{name} callback still passes the test.

@dmitry
dmitry commented Dec 4, 2013

In that case the only possible solution is to move associations into a Topic model. What do you think?

@nurey
nurey commented Dec 4, 2013

I updated the branch and squashed the commits. Test is using Topic model.

@nurey
nurey commented Dec 4, 2013

Unfortunately this makes another test fail. #test_belongs_to_counter_with_reassigning

Need more time to fix this.

@arthurnn
Ruby on Rails member

@nurey Thanks a lot for the PR. This was already fixed on master bf28422 and applied to to 4-0-stable #14410.

Thanks again for the patch. ❤️

@arthurnn arthurnn closed this Mar 18, 2014
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.