提交 13c5aa81 编写于 作者: R Ryuta Kamizono

`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.
上级 46a7b2e2
......@@ -839,10 +839,6 @@ def join_scopes(table, predicate_builder) # :nodoc:
source_reflection.join_scopes(table, predicate_builder) + super
end
def source_type_scope
through_reflection.klass.where(foreign_type => options[:source_type])
end
def has_scope?
scope || options[:source_type] ||
source_reflection.has_scope? ||
......@@ -1011,15 +1007,15 @@ def initialize(reflection, previous_reflection)
def join_scopes(table, predicate_builder) # :nodoc:
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
def constraints
@reflection.constraints + [source_type_info]
@reflection.constraints + [source_type_scope]
end
private
def source_type_info
def source_type_scope
type = @previous_reflection.foreign_type
source_type = @previous_reflection.options[:source_type]
lambda { |object| where(type => source_type) }
......
......@@ -24,6 +24,11 @@
require "models/categorization"
require "models/membership"
require "models/essay"
require "models/hotel"
require "models/department"
require "models/chef"
require "models/cake_designer"
require "models/drink_designer"
class NestedThroughAssociationsTest < ActiveRecord::TestCase
fixtures :authors, :author_addresses, :books, :posts, :subscriptions, :subscribers, :tags, :taggings,
......@@ -574,6 +579,15 @@ def test_nested_has_many_through_should_not_be_autosaved
assert !c.post_taggings.empty?
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
def assert_includes_and_joins_equal(query, expected, association)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册