diff options
author | Arthur Neves <arthurnn@gmail.com> | 2013-09-25 19:06:00 -0400 |
---|---|---|
committer | Arthur Neves <arthurnn@gmail.com> | 2013-09-25 19:06:00 -0400 |
commit | 645af000a4bb0c742063e61d8da01aa5053bc9da (patch) | |
tree | f434f3b2407cd09d335b1deddd2cb846ed2ceeda | |
parent | d1e36842ba8f3eb723fd65535cf292c898c73b68 (diff) | |
download | rails-645af000a4bb0c742063e61d8da01aa5053bc9da.tar.gz rails-645af000a4bb0c742063e61d8da01aa5053bc9da.tar.bz2 rails-645af000a4bb0c742063e61d8da01aa5053bc9da.zip |
fix .find when inverse is set
.find([1]) should return an Array of entries, even when a invese object is in memory already
-rw-r--r-- | activerecord/lib/active_record/associations/collection_association.rb | 9 | ||||
-rw-r--r-- | activerecord/test/cases/associations/has_many_associations_test.rb | 12 |
2 files changed, 16 insertions, 5 deletions
diff --git a/activerecord/lib/active_record/associations/collection_association.rb b/activerecord/lib/active_record/associations/collection_association.rb index 98d573a3a2..b9199f62b9 100644 --- a/activerecord/lib/active_record/associations/collection_association.rb +++ b/activerecord/lib/active_record/associations/collection_association.rb @@ -80,14 +80,13 @@ module ActiveRecord load_target.find(*args) { |*block_args| yield(*block_args) } else if options[:inverse_of] && loaded? - args = args.flatten - raise RecordNotFound, "Couldn't find #{scope.klass.name} without an ID" if args.blank? - + args_flatten = args.flatten + raise RecordNotFound, "Couldn't find #{scope.klass.name} without an ID" if args_flatten.blank? result = find_by_scan(*args) result_size = Array(result).size - if !result || result_size != args.size - scope.raise_record_not_found_exception!(args, result_size, args.size) + if !result || result_size != args_flatten.size + scope.raise_record_not_found_exception!(args_flatten, result_size, args_flatten.size) else result end diff --git a/activerecord/test/cases/associations/has_many_associations_test.rb b/activerecord/test/cases/associations/has_many_associations_test.rb index fd9b129ed4..064e31f634 100644 --- a/activerecord/test/cases/associations/has_many_associations_test.rb +++ b/activerecord/test/cases/associations/has_many_associations_test.rb @@ -337,6 +337,18 @@ class HasManyAssociationsTest < ActiveRecord::TestCase assert_raise(ActiveRecord::RecordNotFound) { firm.clients.find(2, 99) } end + def test_find_ids_and_inverse_of + force_signal37_to_load_all_clients_of_firm + + firm = companies(:first_firm) + client = firm.clients_of_firm.find(3) + assert_kind_of Client, client + + client_ary = firm.clients_of_firm.find([3]) + assert_kind_of Array, client_ary + assert_equal client, client_ary.first + end + def test_find_all firm = Firm.all.merge!(:order => "id").first assert_equal 2, firm.clients.where("#{QUOTED_TYPE} = 'Client'").to_a.length |