Skip to content
This repository was archived by the owner on Mar 30, 2022. It is now read-only.

Commit c370e9e

Browse files
committed
Ordering sequence is now correct when chaining multiple order methods in Rails 4.0
stable branch. Fixes #276. Example: Article.order {id.asc}.order {title.desc}.to_sql # => SELECT "articles".* FROM "articles" ORDER BY "articles".id ASC, # "articles".title DESC
1 parent 918eb10 commit c370e9e

File tree

3 files changed

+10
-3
lines changed

3 files changed

+10
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
* Fix NoMethodError when calling unscope method above Rails 4. By @estum
88
* Fix error when including HABTM or HMT associations without eager loading.
99
Fixes #326.
10+
* Ordering sequence is now correct when chaining multiple order methods. Fixes #276.
1011

1112
## 1.2.1 (2014-07-18)
1213

lib/squeel/adapters/active_record/4.0/relation_extensions.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,10 +160,12 @@ def order!(*args)
160160

161161
# if a symbol is given we prepend the quoted table name
162162
args = args.map { |arg|
163-
arg.is_a?(Symbol) ? "#{quoted_table_name}.#{arg} ASC" : arg
163+
arg.is_a?(Symbol) ?
164+
Arel::Nodes::Ascending.new(klass.arel_table[arg]) :
165+
arg
164166
}
165167

166-
self.order_values = args + self.order_values
168+
self.order_values += args
167169
self
168170
end
169171

spec/squeel/adapters/active_record/relation_extensions_spec.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -776,7 +776,6 @@ module ActiveRecord
776776
if MYSQL_ENV
777777
User.first.groups.to_sql.should match /#{Q}memberships#{Q}.#{Q}active#{Q} = 1/
778778
else
779-
puts User.first.groups.to_sql
780779
User.first.groups.to_sql.should match /#{Q}memberships#{Q}.#{Q}active#{Q} = 't'/
781780
end
782781

@@ -836,6 +835,11 @@ module ActiveRecord
836835
relation.to_sql.should match /ORDER BY #{Q}people#{Q}.#{Q}id#{Q} ASC/
837836
end
838837

838+
it 'orders chain in correct sequence' do
839+
relation = Article.order {id.asc}.order {title.desc}
840+
relation.to_sql.should match /#{Q}articles#{Q}.#{Q}id#{Q} ASC, #{Q}articles#{Q}.#{Q}title#{Q} DESC/
841+
end
842+
839843
end
840844

841845
describe '#reorder' do

0 commit comments

Comments
 (0)