Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

use arel rather than slapping together SQL strings

  • Loading branch information...
commit d345ed40b5783ec2cb43f4434872ea5b2d57d203 1 parent a618e9e
@tenderlove tenderlove authored
View
6 activerecord/lib/active_record/relation/query_methods.rb
@@ -285,8 +285,10 @@ def order!(*args) # :nodoc:
references!(references) if references.any?
# if a symbol is given we prepend the quoted table name
- args = args.map { |arg|
- arg.is_a?(Symbol) ? "#{quoted_table_name}.#{arg} ASC" : arg
+ args = args.map! { |arg|
+ arg.is_a?(Symbol) ?
+ Arel::Nodes::Ascending.new(klass.arel_table[arg]) :
+ arg
}
self.order_values = args + self.order_values
View
9 activerecord/test/cases/relation_test.rb
@@ -201,8 +201,8 @@ def test_relation_merging_with_merged_joins_as_strings
class RelationMutationTest < ActiveSupport::TestCase
class FakeKlass < Struct.new(:table_name, :name)
- def quoted_table_name
- %{"#{table_name}"}
+ def arel_table
+ Post.arel_table
end
end
@@ -224,7 +224,10 @@ def relation
test "#order! with symbol prepends the table name" do
assert relation.order!(:name).equal?(relation)
- assert_equal ['"posts".name ASC'], relation.order_values
+ node = relation.order_values.first
+ assert node.ascending?
+ assert_equal :name, node.expr.name
+ assert_equal "posts", node.expr.relation.name
end
test "#order! on non-string does not attempt regexp match for references" do

2 comments on commit d345ed4

@frodsan

Why? If there any benefits if I apply this pattern to my current apps?

@robin850
Collaborator

@frodsan : See this comment for further information. :wink:

Please sign in to comment.
Something went wrong with that request. Please try again.