aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/relation/query_methods.rb
diff options
context:
space:
mode:
authorRyuta Kamizono <kamipo@gmail.com>2019-03-01 21:27:47 +0900
committerRyuta Kamizono <kamipo@gmail.com>2019-03-01 22:41:46 +0900
commitbff5e799c056a1914653ae53166949b4adff3d4b (patch)
tree03fd528664808e87b9f2cb44a848e4aa195f275a /activerecord/lib/active_record/relation/query_methods.rb
parent0c4bf982e80414a5437f18f988b63885359326e7 (diff)
downloadrails-bff5e799c056a1914653ae53166949b4adff3d4b.tar.gz
rails-bff5e799c056a1914653ae53166949b4adff3d4b.tar.bz2
rails-bff5e799c056a1914653ae53166949b4adff3d4b.zip
Relax table name detection in `from` to allow any extension like INDEX hint
#35360 allows table name qualified if `from` has original table name. But that is still too strict. We have a valid use case that `from` with INDEX hint (e.g. `from("comments USE INDEX (PRIMARY)")`). So I've relaxed the table name detection in `from` to allow any extension like INDEX hint. Fixes #35359.
Diffstat (limited to 'activerecord/lib/active_record/relation/query_methods.rb')
-rw-r--r--activerecord/lib/active_record/relation/query_methods.rb7
1 files changed, 5 insertions, 2 deletions
diff --git a/activerecord/lib/active_record/relation/query_methods.rb b/activerecord/lib/active_record/relation/query_methods.rb
index f88493df8a..24a50db619 100644
--- a/activerecord/lib/active_record/relation/query_methods.rb
+++ b/activerecord/lib/active_record/relation/query_methods.rb
@@ -1091,14 +1091,17 @@ module ActiveRecord
field = klass.attribute_alias(field) if klass.attribute_alias?(field)
from = from_clause.name || from_clause.value
- if klass.columns_hash.key?(field) &&
- (!from || from == table.name || from == connection.quote_table_name(table.name))
+ if klass.columns_hash.key?(field) && (!from || table_name_matches?(from))
arel_attribute(field)
else
yield
end
end
+ def table_name_matches?(from)
+ /(?:\A|(?<!FROM)\s)(?:\b#{table.name}\b|#{connection.quote_table_name(table.name)})(?!\.)/i.match?(from.to_s)
+ end
+
def reverse_sql_order(order_query)
if order_query.empty?
return [arel_attribute(primary_key).desc] if primary_key