Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

The join_nodes must be passed to the JoinDependency initializer and t…

…herefore 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.
  • Loading branch information...
commit 7abf4413366c5bf2b9462ef9dfbd710038b41a6c 1 parent ca26feb
@jonleighton jonleighton authored
View
18 activerecord/lib/active_record/associations/alias_tracker.rb
@@ -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)
View
9 activerecord/lib/active_record/relation/query_methods.rb
@@ -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
View
4 activerecord/test/cases/associations/has_many_through_associations_test.rb
@@ -816,4 +816,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
View
2  activerecord/test/models/toy.rb
@@ -1,4 +1,6 @@
class Toy < ActiveRecord::Base
set_primary_key :toy_id
belongs_to :pet
+
+ scope :with_pet, joins(:pet)
end

1 comment on commit 7abf441

@jonleighton
Collaborator

@tenderlove @spastorino we could consider backporting this to 3-1-0 branch, but my gut feeling at this stage is to err on the side of caution and get it out in 3.1.1, as this isn't a hugely severe bug.

Please sign in to comment.
Something went wrong with that request. Please try again.