Counter cache not updating
26-Feb-2016 09:12= false) @_after_create_counter_called = false elsif attribute_changed? If it helps at all, I discovered that this bug doesn't happen, if the association is updated from something other than nil. this test passes def test_update_from_other_post post = Post.create!
This test passes with Rails 4.1.9 since it does not consider the column to be a counter cache column, however the test fails with Rails 4.2.0 and 4.2.1.rc1 on the last assertion since they consider the column as a counter cache and increment it automatically. https://github.com/rails/rails/blob/3458873642182689e835be8643a50b7634c3e0f5/activerecord/lib/active_record/associations/has_many_association.rb#L83 Here the check to see if it has a cached counter will return true if the default counter cache name exists on the table.
Let’s start by using the same example as in the manual: we want to count the number of comments on the blog post.
The model definitions look like this: The only problem left is that the counts are off if there are already posts and comments in the database.
This is because the counter caches are incremented or decremented when you create or delete an instance of the model.
There are quite a few tests that rely on this, where the model fixtures don't specify counter_cache explicitly.Real world apps could be relying on this current behavior.