mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
reorder method added to ActiveRelation
[#4972 state:committed] Signed-off-by: José Valim <jose.valim@gmail.com>
This commit is contained in:
parent
a2513aea07
commit
65aa6a7db1
2 changed files with 14 additions and 0 deletions
|
@ -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) }
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
Loading…
Reference in a new issue