提交 7e748594 编写于 作者: R Ryuta Kamizono

Association scope's own order should be prioritized over through scope's order

3acc5d6e was changed the order of scope evaluation from through scope to
the association's own scope to be prioritized over the through scope.
But the sorting order will be prioritized that is evaluated first. It is
unintentional effect, association scope's sorting order should be
prioritized as well.

Fixes #32008.
上级 02ffe1b6
......@@ -135,7 +135,7 @@ def add_constraints(scope, owner, chain)
item = eval_scope(reflection, scope_chain_item, owner)
if scope_chain_item == chain_head.scope
scope.merge! item.except(:where, :includes)
scope.merge! item.except(:where, :includes, :unscope, :order)
end
reflection.all_includes do
......@@ -144,7 +144,7 @@ def add_constraints(scope, owner, chain)
scope.unscope!(*item.unscope_values)
scope.where_clause += item.where_clause
scope.order_values |= item.order_values
scope.order_values = item.order_values | scope.order_values
end
end
......
......@@ -1842,6 +1842,13 @@ def test_modifying_a_through_a_has_many_should_raise
].each { |block| assert_raise(ActiveRecord::HasManyThroughCantAssociateThroughHasOneOrManyReflection, &block) }
end
def test_associations_order_should_be_priority_over_throughs_order
david = authors(:david)
expected = [12, 10, 9, 8, 7, 6, 5, 3, 2, 1]
assert_equal expected, david.comments_desc.map(&:id)
assert_equal expected, Author.includes(:comments_desc).find(david.id).comments_desc.map(&:id)
end
def test_dynamic_find_should_respect_association_order_for_through
assert_equal Comment.find(10), authors(:david).comments_desc.where("comments.type = 'SpecialComment'").first
assert_equal Comment.find(10), authors(:david).comments_desc.find_by_type("SpecialComment")
......
......@@ -8,6 +8,7 @@ class Author < ActiveRecord::Base
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 :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"
has_many :posts_with_categories, -> { includes(:categories) }, class_name: "Post"
has_many :posts_with_comments_and_categories, -> { includes(:comments, :categories).order("posts.id") }, class_name: "Post"
......@@ -40,7 +41,7 @@ def ratings
-> { where(title: "Welcome to the weblog").where(Post.arel_table[:comments_count].gt(0)) },
class_name: "Post"
has_many :comments_desc, -> { order("comments.id DESC") }, through: :posts, source: :comments
has_many :comments_desc, -> { order("comments.id DESC") }, through: :posts_sorted_by_id, source: :comments
has_many :unordered_comments, -> { unscope(:order).distinct }, through: :posts_sorted_by_id_limited, source: :comments
has_many :funky_comments, through: :posts, source: :comments
has_many :ordered_uniq_comments, -> { distinct.order("comments.id") }, through: :posts, source: :comments
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册