From 2bdaff4a4e9a3813b4e4e24ff3d3b0aa53b088fc Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Mon, 6 Jun 2005 21:10:59 +0000 Subject: Added a second parameter to the build and create method for has_one that controls whether the existing association should be replaced (which means nullifying its foreign key as well). By default this is true, but false can be passed to prevent it. git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@1392 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- activerecord/lib/active_record/associations.rb | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'activerecord/lib/active_record/associations.rb') diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb index 53a7ea7f11..001572a2c5 100755 --- a/activerecord/lib/active_record/associations.rb +++ b/activerecord/lib/active_record/associations.rb @@ -676,8 +676,9 @@ module ActiveRecord def association_constructor_method(constructor, association_name, association_class_name, association_class_primary_key_name, options, association_proxy_class) define_method("#{constructor}_#{association_name}") do |*params| - attributees = params.first unless params.empty? - association = instance_variable_get("@#{association_name}") + attributees = params.first unless params.empty? + replace_existing = params[1].nil? ? true : params[1] + association = instance_variable_get("@#{association_name}") if association.nil? association = association_proxy_class.new(self, @@ -686,7 +687,7 @@ module ActiveRecord instance_variable_set("@#{association_name}", association) end - association.send(constructor, attributees) + association.send(constructor, attributees, replace_existing) end end -- cgit v1.2.3