提交 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) ...@@ -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 should be downcased as some database adapters (Oracle) return quoted name in uppercase
quoted_name = connection.quote_table_name(name).downcase quoted_name = connection.quote_table_name(name).downcase
table_joins.map { |join| counts = table_joins.map do |join|
# Table names + table aliases if join.is_a?(Arel::Nodes::StringJoin)
join.left.downcase.scan( # Table names + table aliases
/join(?:\s+\w+)?\s+(\S+\s+)?#{quoted_name}\son/ join.left.downcase.scan(
).size /join(?:\s+\w+)?\s+(\S+\s+)?#{quoted_name}\son/
}.sum ).size
else
join.left.table_name == name ? 1 : 0
end
end
counts.sum
end end
def truncate(name) def truncate(name)
......
...@@ -254,12 +254,12 @@ def build_joins(manager, joins) ...@@ -254,12 +254,12 @@ def build_joins(manager, joins)
association_joins = buckets['association_join'] || [] association_joins = buckets['association_join'] || []
stashed_association_joins = buckets['stashed_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| string_joins = (buckets['string_join'] || []).map { |x|
x.strip x.strip
}.uniq }.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( join_dependency = ActiveRecord::Associations::JoinDependency.new(
@klass, @klass,
...@@ -267,10 +267,6 @@ def build_joins(manager, joins) ...@@ -267,10 +267,6 @@ def build_joins(manager, joins)
join_list 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) join_dependency.graft(*stashed_association_joins)
@implicit_readonly = true unless association_joins.empty? && stashed_association_joins.empty? @implicit_readonly = true unless association_joins.empty? && stashed_association_joins.empty?
...@@ -280,7 +276,6 @@ def build_joins(manager, joins) ...@@ -280,7 +276,6 @@ def build_joins(manager, joins)
association.join_to(manager) association.join_to(manager)
end end
manager.join_sources.concat join_nodes.uniq
manager.join_sources.concat join_list manager.join_sources.concat join_list
manager manager
......
...@@ -821,4 +821,8 @@ def test_preloading_empty_through_association_via_joins ...@@ -821,4 +821,8 @@ def test_preloading_empty_through_association_via_joins
assert person.posts.loaded?, 'person.posts should be loaded' assert person.posts.loaded?, 'person.posts should be loaded'
assert_equal [], person.posts assert_equal [], person.posts
end end
def test_explicitly_joining_join_table
assert_equal owners(:blackbeard).toys, owners(:blackbeard).toys.with_pet
end
end end
class Toy < ActiveRecord::Base class Toy < ActiveRecord::Base
set_primary_key :toy_id set_primary_key :toy_id
belongs_to :pet belongs_to :pet
scope :with_pet, joins(:pet)
end end
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册