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 releying on the `inverse_of` property of the relation as well as comparing the models the association points two. Note however that this second check doesn't work for polymorphic associations. Fixes rails#41250 Co-Authored-By: Jean Boussier <jean.boussier@gmail.com>
- Loading branch information
Showing
5 changed files
with
60 additions
and
3 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
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