mirror of
https://github.com/github/rails.git
synced 2026-04-26 03:00:59 -04:00
Improve ordering of multiple columns on postgresql
* Only on postgresql, order("first asc, second asc") was invalid
* Closes #1720
This commit is contained in:
@@ -929,7 +929,7 @@ 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 =~ /^(.+)\s+(ASC|DESC)\s*$/i ? $1 : s }
|
||||
order_columns = orders.collect { |s| s.gsub(/\s+(ASC|DESC)\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}" }
|
||||
|
||||
|
||||
@@ -164,6 +164,13 @@ class RelationTest < ActiveRecord::TestCase
|
||||
assert_equal entrants(:first).name, entrants.first.name
|
||||
end
|
||||
|
||||
def test_finding_with_cross_table_order_and_limit
|
||||
tags = Tag.includes(:taggings) \
|
||||
.order("tags.name asc, taggings.taggable_id asc, REPLACE('abc', taggings.taggable_type, taggings.taggable_type)") \
|
||||
.limit(1).to_a
|
||||
assert_equal 1, tags.length
|
||||
end
|
||||
|
||||
def test_finding_with_complex_order_and_limit
|
||||
tags = Tag.includes(:taggings).order("REPLACE('abc', taggings.taggable_type, taggings.taggable_type)").limit(1).to_a
|
||||
assert_equal 1, tags.length
|
||||
|
||||
Reference in New Issue
Block a user