mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05: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:
parent
8a1319dec0
commit
9734a416fa
2 changed files with 8 additions and 1 deletions
|
@ -940,7 +940,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}" }
|
||||
|
||||
|
|
|
@ -176,6 +176,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
|
||||
|
|
Loading…
Reference in a new issue