Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Backport #5168 to 3-2-stable. Fix a problem that NULLS is ignored by …

…postgresql_adapter.rb while creating distincts.
  • Loading branch information...
commit f137c2b838773be3cfa916e9f7a91773b9245bda 1 parent a252436
@kennyj authored
View
2  activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
@@ -1108,7 +1108,7 @@ def distinct(columns, orders) #:nodoc:
# 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*/i, '') }
+ order_columns = orders.collect { |s| s.gsub(/\s+(ASC|DESC)\s*(NULLS\s+(FIRST|LAST)\s*)?/i, '') }
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}" }
View
5 activerecord/test/cases/adapters/postgresql/postgresql_adapter_test.rb
@@ -179,6 +179,11 @@ def test_substitute_at
assert_equal Arel.sql('$2'), bind
end
+ def test_distinct_with_nulls
+ assert_equal "DISTINCT posts.title, posts.updater_id AS alias_0", @connection.distinct("posts.title", ["posts.updater_id desc nulls first"])
+ assert_equal "DISTINCT posts.title, posts.updater_id AS alias_0", @connection.distinct("posts.title", ["posts.updater_id desc nulls last"])
+ end
+
private
def insert(ctx, data)
binds = data.map { |name, value|
Please sign in to comment.
Something went wrong with that request. Please try again.