From d3303c31c23e7fdbd9e523c628c111ef6bea0e74 Mon Sep 17 00:00:00 2001
From: Ryuta Kamizono <kamipo@gmail.com>
Date: Thu, 18 May 2017 10:42:26 +0900
Subject: Add a test case for overwriting existing condition on associations

Overwriting existing condition on associations has already supported
(23bcc65 for eager loading, 2bfa2c0 for preloading).

Fixes #27724.
Closes #29154.
---
 .../test/cases/associations/has_many_through_associations_test.rb  | 7 +++++++
 activerecord/test/models/post.rb                                   | 5 +++++
 2 files changed, 12 insertions(+)

(limited to 'activerecord')

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 1c2138a3d0..a76159fb99 100644
--- a/activerecord/test/cases/associations/has_many_through_associations_test.rb
+++ b/activerecord/test/cases/associations/has_many_through_associations_test.rb
@@ -946,6 +946,13 @@ class HasManyThroughAssociationsTest < ActiveRecord::TestCase
     end
   end
 
+  def test_has_many_through_polymorphic_with_rewhere
+    post = TaggedPost.create!(title: "Tagged", body: "Post")
+    tag = post.tags.create!(name: "Tag")
+    assert_equal [tag], TaggedPost.preload(:tags).last.tags
+    assert_equal [tag], TaggedPost.eager_load(:tags).last.tags
+  end
+
   def test_has_many_through_polymorphic_with_primary_key_option
     assert_equal [categories(:general)], authors(:david).essay_categories
 
diff --git a/activerecord/test/models/post.rb b/activerecord/test/models/post.rb
index ed64e0ee52..ada277f3b2 100644
--- a/activerecord/test/models/post.rb
+++ b/activerecord/test/models/post.rb
@@ -199,6 +199,11 @@ class FirstPost < ActiveRecord::Base
   has_one  :comment,  foreign_key: :post_id
 end
 
+class TaggedPost < Post
+  has_many :taggings, -> { rewhere(taggable_type: "TaggedPost") }, as: :taggable
+  has_many :tags, through: :taggings
+end
+
 class PostWithDefaultInclude < ActiveRecord::Base
   self.inheritance_column = :disabled
   self.table_name = "posts"
-- 
cgit v1.2.3