forked from rails/rails
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix counter_cache create/concat with overlapping counter_cache_column
Fix when multiple `belongs_to` maps to the same counter_cache column. In such situation `inverse_which_updates_counter_cache` may find the wrong relation which leads into an invalid increment of the counter_cache. This is done by improving the inverse relation detection in 2 ways: - by comparing `inverse.klass` with the current `active_record` (doesn't work for polymorphic) - by relying on `inverse_of` The above adjustments allows to correctly map the `inverse_which_updates_counter_cache` for both non-polymorphic/polymorphic relations. Fixes rails#41250
- Loading branch information
Showing
4 changed files
with
56 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
activerecord/test/models/comment_overlapping_counter_cache.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# frozen_string_literal: true | ||
|
||
class CommentOverlappingCounterCache < ActiveRecord::Base | ||
belongs_to :user_comments_count, counter_cache: :comments_count | ||
belongs_to :post_comments_count, class_name: "PostCommentsCount" | ||
belongs_to :commentable, polymorphic: true, counter_cache: :comments_count | ||
end | ||
|
||
class UserCommentsCount < ActiveRecord::Base | ||
has_many :comments, as: :commentable, class_name: "CommentOverlappingCounterCache" | ||
end | ||
|
||
class PostCommentsCount < ActiveRecord::Base | ||
has_many :comments, class_name: "CommentOverlappingCounterCache" | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters