1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

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
```
This commit is contained in:
Yves Senn 2013-11-13 17:26:55 +01:00
parent a7afceec3e
commit f83c9b10b4
4 changed files with 28 additions and 24 deletions

View file

@ -1,3 +1,7 @@
* Use strings to represent non-string `order_values`.
*Yves Senn*
* Checks to see if the record contains the foreign key to set the inverse automatically.
*Edo Balvers*

View file

@ -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
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

View file

@ -146,5 +146,17 @@ class MergingDifferentRelationsTest < ActiveRecord::TestCase
merge(Author.order(:name)).pluck("authors.name")
assert_equal ["Bob", "Bob", "David"], posts_by_author_name
posts_by_author_name = Post.limit(3).joins(:author).
merge(Author.order("name")).pluck("authors.name")
assert_equal ["Bob", "Bob", "David"], posts_by_author_name
end
test "merging order relations (using a hash argument)" do
posts_by_author_name = Post.limit(4).joins(:author).
merge(Author.order(name: :desc)).pluck("authors.name")
assert_equal ["Mary", "Mary", "Mary", "David"], posts_by_author_name
end
end

View file

@ -7,10 +7,6 @@ module ActiveRecord
extend ActiveRecord::Delegation::DelegateCache
inherited self
def arel_table
Post.arel_table
end
def connection
Post.connection
end
@ -21,7 +17,7 @@ module ActiveRecord
end
def relation
@relation ||= Relation.new FakeKlass.new('posts'), :b
@relation ||= Relation.new FakeKlass.new('posts'), Post.arel_table
end
(Relation::MULTI_VALUE_METHODS - [:references, :extending, :order]).each do |method|