mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Use arel nodes instead of raw sql
This commit is contained in:
parent
2bf65caf56
commit
878db1f4ef
2 changed files with 7 additions and 4 deletions
|
@ -1233,7 +1233,10 @@ module ActiveRecord
|
|||
|
||||
# Construct a clean list of column names from the ORDER BY clause, removing
|
||||
# any ASC/DESC modifiers
|
||||
order_columns = orders.collect { |s| s.gsub(/\s+(ASC|DESC)\s*(NULLS\s+(FIRST|LAST)\s*)?/i, '') }
|
||||
order_columns = orders.collect do |s|
|
||||
s = s.to_sql unless s.is_a?(String)
|
||||
s.gsub(/\s+(ASC|DESC)\s*(NULLS\s+(FIRST|LAST)\s*)?/i, '')
|
||||
end
|
||||
order_columns.delete_if { |c| c.blank? }
|
||||
order_columns = order_columns.zip((0...order_columns.size).to_a).map { |s,i| "#{s} AS alias_#{i}" }
|
||||
|
||||
|
|
|
@ -91,7 +91,7 @@ module ActiveRecord
|
|||
def first(limit = nil)
|
||||
if limit
|
||||
if order_values.empty? && primary_key
|
||||
order("#{quoted_table_name}.#{quoted_primary_key} ASC").limit(limit).to_a
|
||||
order(arel_table[primary_key].asc).limit(limit).to_a
|
||||
else
|
||||
limit(limit).to_a
|
||||
end
|
||||
|
@ -117,7 +117,7 @@ module ActiveRecord
|
|||
def last(limit = nil)
|
||||
if limit
|
||||
if order_values.empty? && primary_key
|
||||
order("#{quoted_table_name}.#{quoted_primary_key} DESC").limit(limit).reverse
|
||||
order(arel_table[primary_key].desc).limit(limit).reverse
|
||||
else
|
||||
to_a.last(limit)
|
||||
end
|
||||
|
@ -362,7 +362,7 @@ module ActiveRecord
|
|||
else
|
||||
@first ||=
|
||||
if order_values.empty? && primary_key
|
||||
order("#{quoted_table_name}.#{quoted_primary_key} ASC").limit(1).to_a.first
|
||||
order(arel_table[primary_key].asc).limit(1).to_a.first
|
||||
else
|
||||
limit(1).to_a.first
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue