counter_cache issue when creating or deleting records via a nested form #10242

Closed
alekseyg opened this Issue Apr 17, 2013 · 9 comments

Projects

None yet

4 participants

@alekseyg

See code to reproduce this issue: https://gist.github.com/anime4christ/5400932

When creating or deleting a record with child records via a nested form, the implicit counter_cache will decrement the cache_column, but will not increment it. The explicit counter_cache will completely fail and not update the cache_column at all.

Here is the output with implicit counter_cache:

Active Record 3.2.13
-- create_table("photos", {:force=>true})
   -> 0.0126s
-- create_table("postings", {:force=>true})
   -> 0.0039s
Create posting with one photo via nested form:
Posting#photos_count: 0; Expected: 1
Delete posting's photo via nested form:
Posting#photos_count: -1; Expected: 0

Here is the output with explicit counter_cache:

Active Record 3.2.13
-- create_table("photos", {:force=>true})
   -> 0.0145s
-- create_table("postings", {:force=>true})
   -> 0.0039s
Create posting with one photo via nested form:
Posting#photos_count: 0; Expected: 1
Delete posting's photo via nested form:
Posting#photos_count: 0; Expected: 0

Here's the diff between the original file and the explicit:

28c28
<   belongs_to :posting

---
>   belongs_to :posting, :counter_cache => true

Edge Rails gives identical output except for the ActiveRecord version number.

@matthewrobertson
Contributor

I tested this out, if you add :counter_cache => true to the belongs_to it works as expected. AR does not actually support implicit counter caches, its just an accident of the implementation.

@alekseyg

OK, so then the so-called implicit counter_cache should be fixed with your pull #8447 then.

So, using counter_cache => true, I'm getting this output with Rails 4.0.0.beta1:

Active Record 4.0.0.beta1
-- create_table("photos", {:force=>true})
   -> 0.0112s
-- create_table("postings", {:force=>true})
   -> 0.0028s
Create posting with one photo via nested form:
Posting#photos_count: 1; Expected: 1
Posting#photos.count: 1; Expected: 1
Posting#photos.size: 1; Expected: 1
Delete posting's photo via nested form:
Posting#photos_count: 1; Expected: 0
Posting#photos.count: 0; Expected: 0
Posting#photos.size: 1; Expected: 0

with this diff (add counter_cache, reload the posting after creating/deleting a photo, and try photos.count, photos.size, and photos_count):

28c28
<   belongs_to :posting
---
>   belongs_to :posting, :counter_cache => true
39a40,41
> posting.reload
> 
41a44,45
> puts "Posting\#photos.count: #{posting.photos.count}; Expected: 1"
> puts "Posting\#photos.size: #{posting.photos.size}; Expected: 1"
44a49,50
> posting.reload
> 
46a53,54
> puts "Posting\#photos.count: #{posting.photos.count}; Expected: 0"
> puts "Posting\#photos.size: #{posting.photos.size}; Expected: 0"

Am I missing something?

@alekseyg

Here's the gist for easier access: https://gist.github.com/anime4christ/5456450

For some reason this increments, but does not decrement.

@matthewrobertson
Contributor

@anime4christ ya you need to reload to see the counter cache update. That is expected behaviour

@alekseyg

@matthewrobertson I just checked it in Edge Rails and Rails 3.2.13 and it works as expected. It is broken in 4.0 Beta 1 only. The only thing left then is for #8447 Is there a way to install edge Rails with that pull request applied to test against it? Just wanted to try it as well.

@matthewrobertson
Contributor

If you use bundler to setup your script you can install Active Record from Github

@alekseyg

@matthewrobertson OK, I can confirm that #8447 fixes this bug completely

@alekseyg alekseyg added the stale label Apr 23, 2014
@rafaelfranca
Member

This issue has been automatically marked as stale because it has not been commented on for at least
three months.

The resources of the Rails team are limited, and so we are asking for your help.

If you can still reproduce this error on the 4-1-stable, 4-0-stable branches or on master,
please reply with all of the information you have about it in order to keep the issue open.

Thank you for all your contributions.

@rails-bot rails-bot closed this May 27, 2014
@rails-bot

This issue has been automatically closed because of inactivity.

If you can still reproduce this error on the 4-1-stable, 4-0-stable branches or on master,
please reply with all of the information you have about it in order to keep the issue open.

Thank you for all your contributions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment