1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Respect through association scopes when used with polymorphic

When the `source_type` option is passed to a has_many through, the
resulting `Reflection` will be an instance of `PolymorphicReflection`
and not `ThroughReflection`, meaning that it will ignore the scopes that
it needs to apply from the through reflections. This adds an additional
delegation call to remedy this. I've been finding the reflection code
completely impenetrable lately, it could use some major love.

Fixes #22726
This commit is contained in:
Sean Griffin 2016-02-29 14:35:19 -07:00
parent ddb7be50e8
commit af2c427c39
2 changed files with 8 additions and 1 deletions

View file

@ -995,7 +995,7 @@ module ActiveRecord
end
def constraints
[source_type_info]
@reflection.constraints + [source_type_info]
end
def source_type_info

View file

@ -363,6 +363,13 @@ class AssociationsJoinModelTest < ActiveRecord::TestCase
assert_equal posts(:welcome, :thinking).sort_by(&:id), tags(:general).tagged_posts.sort_by(&:id)
end
def test_has_many_polymorphic_associations_merges_through_scope
Tag.has_many :null_taggings, -> { none }, class_name: :Tagging
Tag.has_many :null_tagged_posts, :through => :null_taggings, :source => 'taggable', :source_type => 'Post'
assert_equal [], tags(:general).null_tagged_posts
refute_equal [], tags(:general).tagged_posts
end
def test_eager_has_many_polymorphic_with_source_type
tag_with_include = Tag.all.merge!(:includes => :tagged_posts).find(tags(:general).id)
desired = posts(:welcome, :thinking)