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

source_type_scope should respect correct table alias

`join_scopes` in `PolymorphicReflection` is passed aliased `table`, so
it should be respected for `source_type_scope`.

Closes #13969.
Fixes #13920.
Fixes #15190.
This commit is contained in:
Ryuta Kamizono 2017-10-30 22:06:12 +09:00
parent 46a7b2e20c
commit 13c5aa818e
2 changed files with 17 additions and 7 deletions

View file

@ -839,10 +839,6 @@ module ActiveRecord
source_reflection.join_scopes(table, predicate_builder) + super source_reflection.join_scopes(table, predicate_builder) + super
end end
def source_type_scope
through_reflection.klass.where(foreign_type => options[:source_type])
end
def has_scope? def has_scope?
scope || options[:source_type] || scope || options[:source_type] ||
source_reflection.has_scope? || source_reflection.has_scope? ||
@ -1011,15 +1007,15 @@ module ActiveRecord
def join_scopes(table, predicate_builder) # :nodoc: def join_scopes(table, predicate_builder) # :nodoc:
scopes = @previous_reflection.join_scopes(table, predicate_builder) + super scopes = @previous_reflection.join_scopes(table, predicate_builder) + super
scopes << @previous_reflection.source_type_scope scopes << build_scope(table, predicate_builder).instance_exec(nil, &source_type_scope)
end end
def constraints def constraints
@reflection.constraints + [source_type_info] @reflection.constraints + [source_type_scope]
end end
private private
def source_type_info def source_type_scope
type = @previous_reflection.foreign_type type = @previous_reflection.foreign_type
source_type = @previous_reflection.options[:source_type] source_type = @previous_reflection.options[:source_type]
lambda { |object| where(type => source_type) } lambda { |object| where(type => source_type) }

View file

@ -24,6 +24,11 @@ require "models/category"
require "models/categorization" require "models/categorization"
require "models/membership" require "models/membership"
require "models/essay" require "models/essay"
require "models/hotel"
require "models/department"
require "models/chef"
require "models/cake_designer"
require "models/drink_designer"
class NestedThroughAssociationsTest < ActiveRecord::TestCase class NestedThroughAssociationsTest < ActiveRecord::TestCase
fixtures :authors, :author_addresses, :books, :posts, :subscriptions, :subscribers, :tags, :taggings, fixtures :authors, :author_addresses, :books, :posts, :subscriptions, :subscribers, :tags, :taggings,
@ -574,6 +579,15 @@ class NestedThroughAssociationsTest < ActiveRecord::TestCase
assert !c.post_taggings.empty? assert !c.post_taggings.empty?
end end
def test_polymorphic_has_many_through_joined_different_table_twice
cake_designer = CakeDesigner.create!(chef: Chef.new)
drink_designer = DrinkDesigner.create!(chef: Chef.new)
department = Department.create!(chefs: [cake_designer.chef, drink_designer.chef])
hotel = Hotel.create!(departments: [department])
assert_equal hotel, Hotel.joins(:cake_designers, :drink_designers).take
end
private private
def assert_includes_and_joins_equal(query, expected, association) def assert_includes_and_joins_equal(query, expected, association)