aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test
diff options
context:
space:
mode:
authorRyuta Kamizono <kamipo@gmail.com>2017-09-07 07:33:38 +0900
committerRyuta Kamizono <kamipo@gmail.com>2017-09-07 07:43:44 +0900
commit3acc5d6ef7b3f000d0c541b9dc2881a8b7f40f75 (patch)
tree24d85800fdf8b5da44d07fa4fd2a55306914d4c9 /activerecord/test
parent0a94612a979762e1bbeb9cff13322e3f9f721ca3 (diff)
downloadrails-3acc5d6ef7b3f000d0c541b9dc2881a8b7f40f75.tar.gz
rails-3acc5d6ef7b3f000d0c541b9dc2881a8b7f40f75.tar.bz2
rails-3acc5d6ef7b3f000d0c541b9dc2881a8b7f40f75.zip
`has_many :through` with unscope should affect to through scope
The order of scope evaluation should be from through scope to the association's own scope. Otherwise the association's scope cannot affect to through scope. Fixes #13677. Closes #28449.
Diffstat (limited to 'activerecord/test')
-rw-r--r--activerecord/test/cases/associations/has_many_through_associations_test.rb4
-rw-r--r--activerecord/test/models/author.rb1
2 files changed, 5 insertions, 0 deletions
diff --git a/activerecord/test/cases/associations/has_many_through_associations_test.rb b/activerecord/test/cases/associations/has_many_through_associations_test.rb
index f8ea51225a..521b388cee 100644
--- a/activerecord/test/cases/associations/has_many_through_associations_test.rb
+++ b/activerecord/test/cases/associations/has_many_through_associations_test.rb
@@ -1250,6 +1250,10 @@ class HasManyThroughAssociationsTest < ActiveRecord::TestCase
TenantMembership.current_member = nil
end
+ def test_has_many_through_with_unscope_should_affect_to_through_scope
+ assert_equal [comments(:eager_other_comment1)], authors(:mary).unordered_comments
+ end
+
def test_has_many_through_with_scope_should_respect_table_alias
family = Family.create!
users = 3.times.map { User.create! }
diff --git a/activerecord/test/models/author.rb b/activerecord/test/models/author.rb
index 0a52cc5390..3371fcbfcc 100644
--- a/activerecord/test/models/author.rb
+++ b/activerecord/test/models/author.rb
@@ -40,6 +40,7 @@ class Author < ActiveRecord::Base
class_name: "Post"
has_many :comments_desc, -> { order("comments.id DESC") }, through: :posts, 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
has_many :ordered_uniq_comments_desc, -> { distinct.order("comments.id DESC") }, through: :posts, source: :comments