aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/associations
diff options
context:
space:
mode:
authorDavid Heinemeier Hansson <david@loudthinking.com>2005-06-06 21:10:59 +0000
committerDavid Heinemeier Hansson <david@loudthinking.com>2005-06-06 21:10:59 +0000
commit2bdaff4a4e9a3813b4e4e24ff3d3b0aa53b088fc (patch)
treeee3ae5d9b837bc74112e1e699aa1e73f3dd95804 /activerecord/lib/active_record/associations
parent5c2e0fe6491761ba2c55b780663d7f9ec86a62cc (diff)
downloadrails-2bdaff4a4e9a3813b4e4e24ff3d3b0aa53b088fc.tar.gz
rails-2bdaff4a4e9a3813b4e4e24ff3d3b0aa53b088fc.tar.bz2
rails-2bdaff4a4e9a3813b4e4e24ff3d3b0aa53b088fc.zip
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
Diffstat (limited to 'activerecord/lib/active_record/associations')
-rw-r--r--activerecord/lib/active_record/associations/has_one_association.rb18
1 files changed, 18 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/associations/has_one_association.rb b/activerecord/lib/active_record/associations/has_one_association.rb
index d917f0a799..a6a80083f4 100644
--- a/activerecord/lib/active_record/associations/has_one_association.rb
+++ b/activerecord/lib/active_record/associations/has_one_association.rb
@@ -7,6 +7,24 @@ module ActiveRecord
construct_sql
end
+ def create(attributes = {}, replace_existing = true)
+ record = build(attributes, replace_existing)
+ record.save
+ record
+ end
+
+ def build(attributes = {}, replace_existing = true)
+ record = @association_class.new(attributes)
+
+ if replace_existing
+ replace(record, true)
+ else
+ record[@association_class_primary_key_name] = @owner.id unless @owner.new_record?
+ end
+
+ record
+ end
+
def replace(obj, dont_save = false)
load_target
unless @target.nil?