From 2635fa8ec6e42710a758dbded0f95e8b4465888b Mon Sep 17 00:00:00 2001 From: Mike Breen Date: Sun, 25 Oct 2009 07:10:59 -0400 Subject: [PATCH] Allow ActiveRecord's find to take a Range of IDs. --- activerecord/lib/active_record/base.rb | 5 +++-- activerecord/test/cases/finder_test.rb | 4 ++++ 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index 2ec2f73..d80bcab 100755 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -566,6 +566,7 @@ module ActiveRecord #:nodoc: # Person.find(1, 2, 6) # returns an array for objects with IDs in (1, 2, 6) # Person.find([7, 17]) # returns an array for objects with IDs in (7, 17) # Person.find([1]) # returns an array for the object with ID = 1 + # Person.find(1..5) # returns an array for objects with IDs in (1, 2, 3, 4, 5) # Person.find(1, :conditions => "administrator = 1", :order => "created_on DESC") # # Note that returned records may not be in the same order as the ids you @@ -1560,8 +1561,8 @@ module ActiveRecord #:nodoc: expects_array = ids.first.kind_of?(Array) return ids.first if expects_array && ids.first.empty? - ids = ids.flatten.compact.uniq - + ids = ids.sum {|id| Array.wrap(id)}.uniq + case ids.size when 0 raise RecordNotFound, "Couldn't find #{name} without an ID" diff --git a/activerecord/test/cases/finder_test.rb b/activerecord/test/cases/finder_test.rb index f1bede9..6d35c76 100644 --- a/activerecord/test/cases/finder_test.rb +++ b/activerecord/test/cases/finder_test.rb @@ -130,6 +130,10 @@ class FinderTest < ActiveRecord::TestCase assert_equal(1, Topic.find([ 1 ]).length) end + def test_find_by_range + assert_equal 5, Account.find(1, 3..6).length + end + def test_find_by_ids assert_equal 2, Topic.find(1, 2).size assert_equal topics(:second).title, Topic.find([2]).first.title -- 1.6.5