diff --git a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb index 273c165084..68cf495025 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb @@ -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}" } diff --git a/activerecord/lib/active_record/relation/finder_methods.rb b/activerecord/lib/active_record/relation/finder_methods.rb index d13bed95ce..3c9c9c4e84 100644 --- a/activerecord/lib/active_record/relation/finder_methods.rb +++ b/activerecord/lib/active_record/relation/finder_methods.rb @@ -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