aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMarcelo Silveira <marcelo@mhfs.com.br>2012-04-27 20:19:11 -0300
committerMarcelo Silveira <marcelo@mhfs.com.br>2012-05-02 21:25:41 -0300
commit878db1f4ef842a7c6c406aefb4dd2e629b5f47af (patch)
tree4347c59bc41d508b9a3eaeaea69488ce4e5ad332
parent2bf65caf565c5923684953557594fc287c80c6ca (diff)
downloadrails-878db1f4ef842a7c6c406aefb4dd2e629b5f47af.tar.gz
rails-878db1f4ef842a7c6c406aefb4dd2e629b5f47af.tar.bz2
rails-878db1f4ef842a7c6c406aefb4dd2e629b5f47af.zip
Use arel nodes instead of raw sql
-rw-r--r--activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb5
-rw-r--r--activerecord/lib/active_record/relation/finder_methods.rb6
2 files changed, 7 insertions, 4 deletions
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