This project is archived and is in readonly mode.
Custom counter_cache is not used when using collection.size method
-
Jeremy Kemper
- Milestone changed from 2.x to 2.3.4
- State changed from new to open
-
Balint Erdi
I think this is not related with the counter_cache column having the default name or a custom one. I found two tests for this:
def test_counter_cache topic = Topic.create :title => "Zoom-zoom-zoom" assert_equal 0, topic[:replies_count] reply = Reply.create(:title => "re: zoom", :content => "speedy quick!") reply.topic = topic assert_equal 1, topic.reload[:replies_count] assert_equal 1, topic.replies.size topic[:replies_count] = 15 assert_equal 15, topic.replies.size end def test_custom_counter_cache reply = Reply.create(:title => "re: zoom", :content => "speedy quick!") assert_equal 0, reply[:replies_count] silly = SillyReply.create(:title => "gaga", :content => "boo-boo") silly.reply = reply assert_equal 1, reply.reload[:replies_count] assert_equal 1, reply.replies.size reply[:replies_count] = 17 assert_equal 17, reply.replies.size endBoth of these pass, of course. Now if you comment out the line that reloads the "has-many end" of the association,
# assert_equal 1, topic.reload[:replies_count]and
# assert_equal 1, reply.reload[:replies_count]then both of them fail.
Now I guess that the tests serve as specifications, too, so that the collection size should only show the updated value through the counter_cache after a reload. If this is not the case, then probably both cases (tests) have to be redefined and fixed.
-
Balint Erdi
Hmm, but I did manage to recreate the original problem:
With :counter_cache => true :
>> Author.find(a.id).send(:read_attribute, "posts_count") => 1 >> Author.find(a.id).posts.size => 1With :counter_cache => "custom_posts_count":
>> Author.find(a.id).send(:read_attribute, "custom_posts_count") => 1 >> Author.find(a.id).posts.size => 0But failed to write a failing unit test at this point.
-
Balint Erdi
Added failing unit test.
-
Balint Erdi
And here is the patch that fixes it.
-
Balint Erdi
- Tag changed from active_record, collection, counter_cache to active_record, collection, counter_cache, patch
-
Fernando Guillen
+1 I trust completely on Balint.
-
Balint Erdi
@Fernando, I am really happy to hear that, but beyond trust, could you check out the 2-3-stable branch, apply my patch, and tell me if it works? Only if you have the time, of course. Thank you!
-
Balint Erdi
- Tag changed from active_record, collection, counter_cache, patch to 2.3.6, active_record, collection, counter_cache, patch
-
pablo (at teambox)
+1 for the patch
-
Rizwan Reza
- Tag changed from 2.3.6, active_record, collection, counter_cache, patch to 2.3.6, active_record, bugmash, collection, counter_cache, patch
-
Tanja Otto
adapted testcases of Balint Erdi because apply of his diff doesn't work in master. I’ve attached a patch.
-
Tanja Otto
adapted fix of Balint Erdi because apply of his diff doesn't work in master. I’ve attached a patch.
-
Hussein Morsy
+1 verified. The testcase and the fix of Tanja works for the master (3.x)
