rails/rails — pull request #16445 by skanev (2014-08-09 20:44:28+00:00)
Title: Fix counter_cache for polymorphic associations

Fixes #16407.

[ActiveRecord::Association::Builder::BelongsTo][belongs-to] 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:

This reverts commit edd2f21e8095fe4a38e812025b4d9fd0e0cc28f1.

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

[belongs-to]: https://github.com/rails/rails/blob/master/activerecord/lib/active_record/associations/builder/belongs_to.rb#L38