diff options
Diffstat (limited to 'activerecord')
-rw-r--r-- | activerecord/CHANGELOG | 2 | ||||
-rwxr-xr-x | activerecord/lib/active_record/base.rb | 4 | ||||
-rwxr-xr-x | activerecord/test/finder_test.rb | 7 |
3 files changed, 11 insertions, 2 deletions
diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG index 4590f2c1a2..8ff0df6739 100644 --- a/activerecord/CHANGELOG +++ b/activerecord/CHANGELOG @@ -1,5 +1,7 @@ *CVS* +* Fixed that Base#find will return an array if given an array -- regardless of the number of elements #270 [Marten] + * Fixed that has_and_belongs_to_many would generate bad sql when naming conventions differed from using vanilla "id" everywhere [RedTerror] * Added a better exception for when a type column is used in a table without the intention of triggering single-table inheritance. Example: diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index 899db1af10..f6233e5989 100755 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -234,8 +234,10 @@ module ActiveRecord #:nodoc: # Person.find(1) # returns the object for ID = 1 # 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 objects the object with ID = 1 # +RecordNotFound+ is raised if no record can be found. def find(*ids) + expects_array = ids.first.kind_of?(Array) ids = ids.flatten.compact.uniq if ids.length > 1 @@ -253,7 +255,7 @@ module ActiveRecord #:nodoc: sql << " AND #{type_condition}" unless descends_from_active_record? if record = connection.select_one(sql, "#{name} Find") - instantiate(record) + expects_array ? [instantiate(record)] : instantiate(record) else raise RecordNotFound, "Couldn't find #{name} with ID = #{id}" end diff --git a/activerecord/test/finder_test.rb b/activerecord/test/finder_test.rb index ca78fbe651..fbe4ef56f3 100755 --- a/activerecord/test/finder_test.rb +++ b/activerecord/test/finder_test.rb @@ -14,9 +14,14 @@ class FinderTest < Test::Unit::TestCase assert_equal(@topic_fixtures["first"]["title"], Topic.find(1).title) end + def test_find_by_array_of_one_id + assert_kind_of(Array, Topic.find([ 1 ])) + assert_equal(1, Topic.find([ 1 ]).length) + end + def test_find_by_ids assert_equal(2, Topic.find(1, 2).length) - assert_equal(@topic_fixtures["second"]["title"], Topic.find([ 2 ]).title) + assert_equal(@topic_fixtures["second"]["title"], Topic.find([ 2 ]).first.title) end def test_find_by_ids_missing_one |