提交 735d985b 编写于 作者: J Jon Leighton

The join_nodes must be passed to the JoinDependency initializer and therefore...

The join_nodes must be passed to the JoinDependency initializer and therefore counted by the alias tracker. This is because the association_joins are aliased on initialization and then the tables are cached, so it is no use to alias the join_nodes later. Fixes #2556.
上级 fd22d040
......@@ -53,12 +53,18 @@ def initial_count_for(name)
# quoted_name should be downcased as some database adapters (Oracle) return quoted name in uppercase
quoted_name = connection.quote_table_name(name).downcase
table_joins.map { |join|
# Table names + table aliases
join.left.downcase.scan(
/join(?:\s+\w+)?\s+(\S+\s+)?#{quoted_name}\son/
).size
}.sum
counts = table_joins.map do |join|
if join.is_a?(Arel::Nodes::StringJoin)
# Table names + table aliases
join.left.downcase.scan(
/join(?:\s+\w+)?\s+(\S+\s+)?#{quoted_name}\son/
).size
else
join.left.table_name == name ? 1 : 0
end
end
counts.sum
end
def truncate(name)
......
......@@ -254,12 +254,12 @@ def build_joins(manager, joins)
association_joins = buckets['association_join'] || []
stashed_association_joins = buckets['stashed_join'] || []
join_nodes = buckets['join_node'] || []
join_nodes = (buckets['join_node'] || []).uniq
string_joins = (buckets['string_join'] || []).map { |x|
x.strip
}.uniq
join_list = custom_join_ast(manager, string_joins)
join_list = join_nodes + custom_join_ast(manager, string_joins)
join_dependency = ActiveRecord::Associations::JoinDependency.new(
@klass,
......@@ -267,10 +267,6 @@ def build_joins(manager, joins)
join_list
)
join_nodes.each do |join|
join_dependency.alias_tracker.aliased_name_for(join.left.name.downcase)
end
join_dependency.graft(*stashed_association_joins)
@implicit_readonly = true unless association_joins.empty? && stashed_association_joins.empty?
......@@ -280,7 +276,6 @@ def build_joins(manager, joins)
association.join_to(manager)
end
manager.join_sources.concat join_nodes.uniq
manager.join_sources.concat join_list
manager
......
......@@ -821,4 +821,8 @@ def test_preloading_empty_through_association_via_joins
assert person.posts.loaded?, 'person.posts should be loaded'
assert_equal [], person.posts
end
def test_explicitly_joining_join_table
assert_equal owners(:blackbeard).toys, owners(:blackbeard).toys.with_pet
end
end
class Toy < ActiveRecord::Base
set_primary_key :toy_id
belongs_to :pet
scope :with_pet, joins(:pet)
end
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册