From 8b1265cfc5d5c82330b7e9975a6c1cd79ac0ba70 Mon Sep 17 00:00:00 2001 From: Jeremy Friesen Date: Mon, 16 May 2011 15:19:50 -0400 Subject: [PATCH] Updated ActiveRecord::Base.find_or_instantiator_by_attributes method to handle two attributes in the finder method name with only one attribute being provided. Lighthouse #6464-diffrent-behaviour-for-find_or_create_by-and-find_by --- .../lib/active_record/relation/finder_methods.rb | 7 ++++++- activerecord/test/cases/finder_test.rb | 7 +++++++ 2 files changed, 13 insertions(+), 1 deletions(-) diff --git a/activerecord/lib/active_record/relation/finder_methods.rb b/activerecord/lib/active_record/relation/finder_methods.rb index 32d1cff..5dabd87 100644 --- a/activerecord/lib/active_record/relation/finder_methods.rb +++ b/activerecord/lib/active_record/relation/finder_methods.rb @@ -272,8 +272,13 @@ module ActiveRecord unprotected_attributes_for_create[attributes[i]] = args[i] end end + attributes_for_create = protected_attributes_for_create.merge(unprotected_attributes_for_create) - conditions = (protected_attributes_for_create.merge(unprotected_attributes_for_create)).slice(*attributes).symbolize_keys + if attributes_for_create.length < attributes.length + attributes.each { |attrib| attributes_for_create[attrib] ||= nil } + end + + conditions = (attributes_for_create).slice(*attributes).symbolize_keys record = where(conditions).first diff --git a/activerecord/test/cases/finder_test.rb b/activerecord/test/cases/finder_test.rb index 4e75eaf..384b090 100644 --- a/activerecord/test/cases/finder_test.rb +++ b/activerecord/test/cases/finder_test.rb @@ -815,6 +815,13 @@ class FinderTest < ActiveRecord::TestCase assert created_customer.persisted? end + def test_find_or_create_from_two_attributes_with_one_not_included + assert_nil Author.find_by_name_and_author_address_id('David') + assert_difference 'Author.count' do + Author.find_or_create_by_name_and_author_address_id('David') + end + end + def test_find_or_create_from_one_attribute_and_hash number_of_companies = Company.count sig38 = Company.find_or_create_by_name({:name => "38signals", :firm_id => 17, :client_of => 23}) -- 1.7.4.1