Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #14226 from senny/14109_partial_revert

`includes` uses SQL parsing when String joins are involved.
  • Loading branch information...
commit f8646f0af4b1fb7c6a18ea568664d781c0b54b20 1 parent f7cf45a
@tenderlove tenderlove authored
View
7 activerecord/CHANGELOG.md
@@ -1,3 +1,10 @@
+* `includes` is able to detect the right preloading strategy when string
+ joins are involved.
+
+ Fixes #14109.
+
+ *Aaron Patterson*, *Yves Senn*
+
* Fixed error with validation with enum fields for records where the
value for any enum attribute is always evaluated as 0 during
uniqueness validation.
View
11 activerecord/lib/active_record/relation.rb
@@ -617,7 +617,9 @@ def exec_queries
def references_eager_loaded_tables?
joined_tables = arel.join_sources.map do |join|
- unless join.is_a?(Arel::Nodes::StringJoin)
+ if join.is_a?(Arel::Nodes::StringJoin)
+ tables_in_string(join.left)
+ else
[join.left.table_name, join.left.table_alias]
end
end
@@ -629,5 +631,12 @@ def references_eager_loaded_tables?
(references_values - joined_tables).any?
end
+
+ def tables_in_string(string)
+ return [] if string.blank?
+ # always convert table names to downcase as in Oracle quoted table names are in uppercase
+ # ignore raw_sql_ that is used by Oracle adapter as alias for limit/offset subqueries
+ string.scan(/([a-zA-Z_][.\w]+).?\./).flatten.map{ |s| s.downcase }.uniq - ['raw_sql_']
+ end
end
end
View
9 activerecord/test/cases/associations/eager_test.rb
@@ -1194,4 +1194,13 @@ def test_deep_including_through_habtm
authors(:david).essays.includes(:writer).any?
end
end
+
+ test "preloading associations with string joins and order references" do
+ author = assert_queries(2) {
+ Author.includes(:posts).joins("LEFT JOIN posts ON posts.author_id = authors.id").order("posts.title DESC").first
+ }
+ assert_no_queries {
+ assert_equal 5, author.posts.size
+ }
+ end
end
Please sign in to comment.
Something went wrong with that request. Please try again.