diff options
-rw-r--r-- | activerecord/lib/active_record/relation/query_methods.rb | 8 | ||||
-rw-r--r-- | activerecord/test/cases/relations_test.rb | 6 |
2 files changed, 14 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/relation/query_methods.rb b/activerecord/lib/active_record/relation/query_methods.rb index 4dbb30c777..f7c4c7991b 100644 --- a/activerecord/lib/active_record/relation/query_methods.rb +++ b/activerecord/lib/active_record/relation/query_methods.rb @@ -21,6 +21,14 @@ module ActiveRecord CEVAL end + def reorder(*args, &block) + new_relation = clone + new_relation.send(:apply_modules, Module.new(&block)) if block_given? + value = Array.wrap(args.flatten).reject {|x| x.blank? } + new_relation.order_values = value if value.present? + new_relation + end + def select(*args) if block_given? to_a.select { |*block_args| yield(*block_args) } diff --git a/activerecord/test/cases/relations_test.rb b/activerecord/test/cases/relations_test.rb index 821b4585a7..d37f7bd09c 100644 --- a/activerecord/test/cases/relations_test.rb +++ b/activerecord/test/cases/relations_test.rb @@ -116,6 +116,12 @@ class RelationTest < ActiveRecord::TestCase assert_equal topics(:fourth).title, topics.first.title end + def test_finding_with_reorder + topics = Topic.order('author_name').order('title').reorder('id') + assert_equal 4, topics.to_a.size + assert_equal topics(:first).title, topics.first.title + end + def test_finding_with_order_and_take entrants = Entrant.order("id ASC").limit(2).to_a |