1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

includes uses SQL parsing when String joins are involved.

This is a partial revert of 22b3481ba2.
The current implementation of `references_eager_loaded_tables?` needs to know
every table involved in the query. With the current API this is not possible
without SQL parsing.

While a2dab46cae deprecated SQL parsing for `includes`.
It did not issue deprecation warnings when String joins are involved. This resulted
in a breaking change after the deprecated behavior was removed (22b3481ba2).

We will need to rethink the usage of `includes`, `preload` and `eager_load` but for now,
this brings back the old *working* behavior.
This commit is contained in:
Yves Senn 2014-02-28 09:49:52 +01:00
parent 544c78a4e9
commit d1e7cd14c2
3 changed files with 26 additions and 1 deletions

View file

@ -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 file

@ -617,7 +617,9 @@ module ActiveRecord
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 @@ module ActiveRecord
(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 file

@ -1194,4 +1194,13 @@ class EagerAssociationTest < ActiveRecord::TestCase
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