-
Notifications
You must be signed in to change notification settings - Fork 88
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
counter_cache callbacks called twice #63
Comments
Thanks, I can reproduce the issue. |
OK, it looks like the |
This looks to me to be a rails counter cache bug:
|
I have created a failing test case for activerecord and will open an upstream bug. There is already a working fix in rails/rails#23357, but it is not yet merged. |
Reported upstream issue rails/rails#28203. |
I'm not sure if there's anything we can do about this problem in If a fix is merged into rails we could disable our I'm open to ideas on how to implement a workaround in |
Thanks for your help on this @felixbuenemann we're in Rails 5.0.1 and will upgrade to 5.1 as soon as it's out, I'm really not sure what a good workaround would be inside |
@etdsoft Maybe you could switch your controller code to use |
Unfortunately And following the steps in the description of this issue (e.g. letting Rails generate the scaffold, the controller gets created using It seems that we'll just have to wait it out... |
And then follow by another increment by update_parents_counter_cache, causing the counter cache value to be updated twice. Here are the binding.pry in ActiveRecord and ActsAsTree: which are hit:
|
Does it work correctly, if you override the def update_parents_counter_cache
end Or is the decrement not done? In that case you could try: def update_parents_counter_cache
counter_cache_column = self.class.children_counter_cache_column
if parent_id_changed?
self.class.decrement_counter(counter_cache_column, parent_id_was)
# self.class.increment_counter(counter_cache_column, parent_id)
end
end I'm no longer trying to contribute fixes to rails, it's just too painful. |
With Rails 4.2.7.1, and acts_as_tree 2.7.1, it does seem to work correctly if I override the I tried out,
In this case, the
In this case, the |
Gem version: 2.6.1
Rails version: 5.0.1
The Node class:
Create the following structure using the browser ( a -> 1 ; a.1 -> 2; a.2 -> 3; b -> 4 ):
Then through the browser move
a.2
underb
, browse to/nodes/3/edit
update the parent_id to 4 (b
node) and submit the form. This is what I get in the log:It seems like the counter_cache callbacks are being called twice. After this operation
:children_count
fora
is now 0 and children count forb
is now 2 when they should be 1 fora
and 1 forb
.What am I missing? Thanks!
The text was updated successfully, but these errors were encountered: