Skip to content
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

Fix counter_cache for polymorphic associations #16445

Conversation

skanev
Copy link
Contributor

@skanev skanev commented Aug 9, 2014

Fixes #16407.

ActiveRecord::Association::Builder::BelongsTo does not take into account polymorphic relationships. This change takes care of that.

Unfortunately, the fix broke a test. Fortunately, the test depended on this bug being present. I've removed it and detailed the reasons in the commit message. I'm copying it here for completeness' sake:

At this time, counter_cache does not work with polymorphic relationships
(which is a bug). The test was added to make sure that no
StaleObjectError is raised when the car is destroyed. No such error is
currently raised because the lock version is not incremented by
appending a wheel to the car.

Furthermore, `assert_difference` succeeds because `car.wheels.count`
does not check the counter cache, but the collection size. The test will
fail if it is replaced with `car.wheels_count || 0`.

/cc @senny

@@ -1,3 +1,10 @@
* Fix counter_cache to work with polymorphic relations. This appears to have
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'This appears to have been broken for a while.' is not necessary in the CHANGELOG.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed it from there (and also rebased).

Also removes a false positive test that depends on the fixed bug:

At this time, counter_cache does not work with polymorphic relationships
(which is a bug). The test was added to make sure that no
StaleObjectError is raised when the car is destroyed. No such error is
currently raised because the lock version is not incremented by
appending a wheel to the car.

Furthermore, `assert_difference` succeeds because `car.wheels.count`
does not check the counter cache, but the collection size. The test will
fail if it is replaced with `car.wheels_count || 0`.
sgrif added a commit that referenced this pull request Jul 19, 2015
Since the counter cache was properly being updated, the model became
stale. Simply reloading the model before attempting to destroy is
sufficient for this case. I believe this is enough of an edge case to be
a valid change to the tests, even though it represents a potential
breaking change.
sgrif added a commit that referenced this pull request Jul 19, 2015
@sgrif
Copy link
Contributor

sgrif commented Jul 19, 2015

Sorry for the delay on this. I have manually rebased, fixed some issues, and merged in 6765d48. Note that 8cd1d5a demonstrates that this caused a minor but subtle breaking change. It's fine for 5.0, but I'm not going to backport this to 4-2-stable. (Although I can be persuaded on this matter)

@sgrif sgrif closed this Jul 19, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Bug: Counter cache not working for polymorphic relationship
4 participants