diff options
-rw-r--r-- | activerecord/CHANGELOG | 2 | ||||
-rwxr-xr-x | activerecord/lib/active_record/base.rb | 2 | ||||
-rwxr-xr-x | activerecord/lib/active_record/validations.rb | 1 | ||||
-rwxr-xr-x | activerecord/test/associations_test.rb | 7 |
4 files changed, 10 insertions, 2 deletions
diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG index 0291d96a72..d94ae721e7 100644 --- a/activerecord/CHANGELOG +++ b/activerecord/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* Apply scoping during initialize instead of create. Fixes setting of foreign key when using find_or_initialize_by with scoping. [Cody Fauser] + * SQLServer: handle [quoted] table names. #6635 [rrich] * acts_as_nested_set works with single-table inheritance. #6030 [Josh Susser] diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index c51564ecfc..a783feafed 100755 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -447,7 +447,6 @@ module ActiveRecord #:nodoc: attributes.collect { |attr| create(attr) } else object = new(attributes) - scope(:create).each { |att,value| object.send("#{att}=", value) } if scoped?(:create) object.save object end @@ -1504,6 +1503,7 @@ module ActiveRecord #:nodoc: @new_record = true ensure_proper_type self.attributes = attributes unless attributes.nil? + self.class.send(:scope, :create).each { |att,value| self.send("#{att}=", value) } if self.class.send(:scoped?, :create) yield self if block_given? end diff --git a/activerecord/lib/active_record/validations.rb b/activerecord/lib/active_record/validations.rb index 6f284242cd..b8dc5009ea 100755 --- a/activerecord/lib/active_record/validations.rb +++ b/activerecord/lib/active_record/validations.rb @@ -721,7 +721,6 @@ module ActiveRecord attributes.collect { |attr| create(attr) } else object = new(attributes) - scope(:create).each { |att,value| object.send("#{att}=", value) } if scoped?(:create) object.save! object end diff --git a/activerecord/test/associations_test.rb b/activerecord/test/associations_test.rb index 522a0739e6..4315570c5c 100755 --- a/activerecord/test/associations_test.rb +++ b/activerecord/test/associations_test.rb @@ -750,6 +750,13 @@ class HasManyAssociationsTest < Test::Unit::TestCase companies(:first_firm).clients_of_firm.create([{"name" => "Another Client"}, {"name" => "Another Client II"}]) assert_equal 3, companies(:first_firm).clients_of_firm(true).size end + + def test_find_or_initialize + the_client = companies(:first_firm).clients.find_or_initialize_by_name("Yet another client") + assert_equal companies(:first_firm).id, the_client.firm_id + assert_equal "Yet another client", the_client.name + assert the_client.new_record? + end def test_find_or_create number_of_clients = companies(:first_firm).clients.size |