aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/relation/query_methods.rb
diff options
context:
space:
mode:
authorYves Senn <yves.senn@gmail.com>2013-11-13 17:26:55 +0100
committerYves Senn <yves.senn@gmail.com>2013-11-19 17:40:21 +0100
commitf83c9b10b4c92b0d8deacb30d6fdfa2b1252d6dd (patch)
tree2320f2867a13a1f30faddadb92eb65c98bec8597 /activerecord/lib/active_record/relation/query_methods.rb
parenta7afceec3e491bbf505c690b3019445593c04f68 (diff)
downloadrails-f83c9b10b4c92b0d8deacb30d6fdfa2b1252d6dd.tar.gz
rails-f83c9b10b4c92b0d8deacb30d6fdfa2b1252d6dd.tar.bz2
rails-f83c9b10b4c92b0d8deacb30d6fdfa2b1252d6dd.zip
use arel nodes to represent non-string `order_values`.
This fixes a bug when merging relations of different classes. ``` Given: Post.joins(:author).merge(Author.order(name: :desc)).to_sql Before: SELECT "posts".* FROM "posts" INNER JOIN "authors" ON "authors"."id" = "posts"."author_id" ORDER BY "posts"."name" DESC After: SELECT "posts".* FROM "posts" INNER JOIN "authors" ON "authors"."id" = "posts"."author_id" ORDER BY "authors"."name" DESC ```
Diffstat (limited to 'activerecord/lib/active_record/relation/query_methods.rb')
-rw-r--r--activerecord/lib/active_record/relation/query_methods.rb30
1 files changed, 11 insertions, 19 deletions
diff --git a/activerecord/lib/active_record/relation/query_methods.rb b/activerecord/lib/active_record/relation/query_methods.rb
index 62c555f6d6..b6a7c25b4b 100644
--- a/activerecord/lib/active_record/relation/query_methods.rb
+++ b/activerecord/lib/active_record/relation/query_methods.rb
@@ -995,12 +995,6 @@ module ActiveRecord
s.strip!
s.gsub!(/\sasc\Z/i, ' DESC') || s.gsub!(/\sdesc\Z/i, ' ASC') || s.concat(' DESC')
end
- when Symbol
- { o => :desc }
- when Hash
- o.each_with_object({}) do |(field, dir), memo|
- memo[field] = (dir == :asc ? :desc : :asc )
- end
else
o
end
@@ -1016,17 +1010,6 @@ module ActiveRecord
orders.reject!(&:blank?)
orders = reverse_sql_order(orders) if reverse_order_value
- orders = orders.flat_map do |order|
- case order
- when Symbol
- table[order].asc
- when Hash
- order.map { |field, dir| table[field].send(dir) }
- else
- order
- end
- end
-
arel.order(*orders) unless orders.empty?
end
@@ -1048,8 +1031,17 @@ module ActiveRecord
# if a symbol is given we prepend the quoted table name
order_args.map! do |arg|
- arg.is_a?(Symbol) ? Arel::Nodes::Ascending.new(klass.arel_table[arg]) : arg
- end
+ case arg
+ when Symbol
+ table[arg].asc
+ when Hash
+ arg.map { |field, dir|
+ table[field].send(dir)
+ }
+ else
+ arg
+ end
+ end.flatten!
end
# Checks to make sure that the arguments are not blank. Note that if some