From 13160269fe73e108d50258ba4199464ca99f9c22 Mon Sep 17 00:00:00 2001 From: Ryuta Kamizono Date: Mon, 6 Jul 2020 11:56:01 +0900 Subject: [PATCH] Resolve attribute alias for counter cache column The lack of ability for making this has resolved by #39496 and #39530. --- .../active_record/associations/has_many_association.rb | 4 ++-- activerecord/lib/active_record/reflection.rb | 2 +- .../cases/associations/has_many_associations_test.rb | 2 +- activerecord/test/cases/relations_test.rb | 4 ++-- activerecord/test/fixtures/other_posts.yml | 2 +- activerecord/test/fixtures/posts.yml | 10 +++++----- activerecord/test/models/author.rb | 6 +++--- activerecord/test/models/post.rb | 1 + activerecord/test/schema/schema.rb | 2 +- 9 files changed, 17 insertions(+), 16 deletions(-) diff --git a/activerecord/lib/active_record/associations/has_many_association.rb b/activerecord/lib/active_record/associations/has_many_association.rb index dd2ed55279..eb2f1c6c0a 100644 --- a/activerecord/lib/active_record/associations/has_many_association.rb +++ b/activerecord/lib/active_record/associations/has_many_association.rb @@ -52,7 +52,7 @@ module ActiveRecord # the loaded flag is set to true as well. def count_records count = if reflection.has_cached_counter? - owner._read_attribute(reflection.counter_cache_column).to_i + owner.read_attribute(reflection.counter_cache_column).to_i else scope.count(:all) end @@ -75,7 +75,7 @@ module ActiveRecord if reflection.counter_must_be_updated_by_has_many? counter = reflection.counter_cache_column owner.increment(counter, difference) - owner.send(:clear_attribute_change, counter) # eww + owner.send(:"clear_#{counter}_change") end end diff --git a/activerecord/lib/active_record/reflection.rb b/activerecord/lib/active_record/reflection.rb index 4a0a1ede20..2f4f6a6323 100644 --- a/activerecord/lib/active_record/reflection.rb +++ b/activerecord/lib/active_record/reflection.rb @@ -266,7 +266,7 @@ module ActiveRecord def has_cached_counter? options[:counter_cache] || inverse_which_updates_counter_cache && inverse_which_updates_counter_cache.options[:counter_cache] && - !!active_record.columns_hash[counter_cache_column] + active_record.has_attribute?(counter_cache_column) end def counter_must_be_updated_by_has_many? diff --git a/activerecord/test/cases/associations/has_many_associations_test.rb b/activerecord/test/cases/associations/has_many_associations_test.rb index 8ac9b3beb6..f243f5093f 100644 --- a/activerecord/test/cases/associations/has_many_associations_test.rb +++ b/activerecord/test/cases/associations/has_many_associations_test.rb @@ -48,7 +48,7 @@ class HasManyAssociationsTestForReorderWithJoinDependency < ActiveRecord::TestCa author = authors(:david) # this can fail on adapters which require ORDER BY expressions to be included in the SELECT expression # if the reorder clauses are not correctly handled - assert author.posts_with_comments_sorted_by_comment_id.where("comments.id > 0").reorder("posts.comments_count DESC", "posts.tags_count DESC").last + assert author.posts_with_comments_sorted_by_comment_id.where("comments.id > 0").reorder("posts.comments_count": :desc, "posts.tags_count": :desc).last end end diff --git a/activerecord/test/cases/relations_test.rb b/activerecord/test/cases/relations_test.rb index 28b30ff91e..057725fb82 100644 --- a/activerecord/test/cases/relations_test.rb +++ b/activerecord/test/cases/relations_test.rb @@ -658,10 +658,10 @@ class RelationTest < ActiveRecord::TestCase end def test_includes_with_select - query = Post.select("comments_count AS ranking").order("ranking").includes(:comments) + query = Post.select("legacy_comments_count AS ranking").order("ranking").includes(:comments) .where(comments: { id: 1 }) - assert_equal ["comments_count AS ranking"], query.select_values + assert_equal ["legacy_comments_count AS ranking"], query.select_values assert_equal 1, query.to_a.size end diff --git a/activerecord/test/fixtures/other_posts.yml b/activerecord/test/fixtures/other_posts.yml index 3e11a33802..4290b85016 100644 --- a/activerecord/test/fixtures/other_posts.yml +++ b/activerecord/test/fixtures/other_posts.yml @@ -5,4 +5,4 @@ second_welcome: author_id: 1 title: Welcome to the another weblog body: It's really nice today - comments_count: 1 + legacy_comments_count: 1 diff --git a/activerecord/test/fixtures/posts.yml b/activerecord/test/fixtures/posts.yml index 297dfd57a1..b1856cd097 100644 --- a/activerecord/test/fixtures/posts.yml +++ b/activerecord/test/fixtures/posts.yml @@ -3,7 +3,7 @@ welcome: author_id: 1 title: Welcome to the weblog body: Such a lovely day - comments_count: 2 + legacy_comments_count: 2 tags_count: 1 type: Post @@ -12,7 +12,7 @@ thinking: author_id: 1 title: So I was thinking body: Like I hopefully always am - comments_count: 1 + legacy_comments_count: 1 tags_count: 1 type: SpecialPost @@ -28,7 +28,7 @@ sti_comments: author_id: 1 title: sti comments body: hello - comments_count: 5 + legacy_comments_count: 5 type: Post sti_post_and_comments: @@ -36,7 +36,7 @@ sti_post_and_comments: author_id: 1 title: sti me body: hello - comments_count: 2 + legacy_comments_count: 2 type: StiPost sti_habtm: @@ -52,7 +52,7 @@ eager_other: title: eager loading with OR'd conditions body: hello type: Post - comments_count: 1 + legacy_comments_count: 1 tags_count: 3 misc_by_bob: diff --git a/activerecord/test/models/author.rb b/activerecord/test/models/author.rb index 5a26d587f6..243338f9c2 100644 --- a/activerecord/test/models/author.rb +++ b/activerecord/test/models/author.rb @@ -6,7 +6,7 @@ class Author < ActiveRecord::Base has_one :post has_many :very_special_comments, through: :posts has_many :posts_with_comments, -> { includes(:comments) }, class_name: "Post" - has_many :popular_grouped_posts, -> { includes(:comments).group("type").having("SUM(comments_count) > 1").select("type") }, class_name: "Post" + has_many :popular_grouped_posts, -> { includes(:comments).group("type").having("SUM(legacy_comments_count) > 1").select("type") }, class_name: "Post" has_many :posts_with_comments_sorted_by_comment_id, -> { includes(:comments).order("comments.id") }, class_name: "Post" has_many :posts_sorted_by_id, -> { order(:id) }, class_name: "Post" has_many :posts_sorted_by_id_limited, -> { order("posts.id").limit(1) }, class_name: "Post" @@ -35,10 +35,10 @@ class Author < ActiveRecord::Base has_many :welcome_posts, -> { where(title: "Welcome to the weblog") }, class_name: "Post" has_many :welcome_posts_with_one_comment, - -> { where(title: "Welcome to the weblog").where("comments_count = ?", 1) }, + -> { where(title: "Welcome to the weblog").where(comments_count: 1) }, class_name: "Post" has_many :welcome_posts_with_comments, - -> { where(title: "Welcome to the weblog").where(Post.arel_table[:comments_count].gt(0)) }, + -> { where(title: "Welcome to the weblog").where("comments_count >": 0) }, class_name: "Post" has_many :comments_desc, -> { order("comments.id DESC") }, through: :posts_sorted_by_id, source: :comments diff --git a/activerecord/test/models/post.rb b/activerecord/test/models/post.rb index ac01e0495e..5d3c96050f 100644 --- a/activerecord/test/models/post.rb +++ b/activerecord/test/models/post.rb @@ -24,6 +24,7 @@ class Post < ActiveRecord::Base end alias_attribute :text, :body + alias_attribute :comments_count, :legacy_comments_count scope :containing_the_letter_a, -> { where("body LIKE '%a%'") } scope :titled_with_an_apostrophe, -> { where("title LIKE '%''%'") } diff --git a/activerecord/test/schema/schema.rb b/activerecord/test/schema/schema.rb index e85df8e190..1af732d1c9 100644 --- a/activerecord/test/schema/schema.rb +++ b/activerecord/test/schema/schema.rb @@ -740,7 +740,7 @@ ActiveRecord::Schema.define do t.text :body, null: false end t.string :type - t.integer :comments_count, default: 0 + t.integer :legacy_comments_count, default: 0 t.integer :taggings_with_delete_all_count, default: 0 t.integer :taggings_with_destroy_count, default: 0 t.integer :tags_count, default: 0