mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Resolve attribute alias for counter cache column
The lack of ability for making this has resolved by #39496 and #39530.
This commit is contained in:
parent
b806450037
commit
13160269fe
9 changed files with 17 additions and 16 deletions
|
@ -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
|
||||
|
||||
|
|
|
@ -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?
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
2
activerecord/test/fixtures/other_posts.yml
vendored
2
activerecord/test/fixtures/other_posts.yml
vendored
|
@ -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
|
||||
|
|
10
activerecord/test/fixtures/posts.yml
vendored
10
activerecord/test/fixtures/posts.yml
vendored
|
@ -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:
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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 '%''%'") }
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue