aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record
diff options
context:
space:
mode:
authorJon Leighton <j@jonathanleighton.com>2011-01-09 19:09:51 +0000
committerAaron Patterson <aaron.patterson@gmail.com>2011-01-11 13:45:09 -0800
commit552df9b933e05a3c1d2508c316f1f2bd240accc5 (patch)
tree116f9b2bc884e4913859f0baa0b9c28b79d9e76a /activerecord/lib/active_record
parentd88caa6e4a8f0f64601fc8bf07b61b682263d712 (diff)
downloadrails-552df9b933e05a3c1d2508c316f1f2bd240accc5.tar.gz
rails-552df9b933e05a3c1d2508c316f1f2bd240accc5.tar.bz2
rails-552df9b933e05a3c1d2508c316f1f2bd240accc5.zip
Support for create_association! for has_one associations
Diffstat (limited to 'activerecord/lib/active_record')
-rw-r--r--activerecord/lib/active_record/associations.rb1
-rw-r--r--activerecord/lib/active_record/associations/has_one_association.rb5
2 files changed, 5 insertions, 1 deletions
diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb
index 2e0f54e505..a03d1bbb06 100644
--- a/activerecord/lib/active_record/associations.rb
+++ b/activerecord/lib/active_record/associations.rb
@@ -1536,6 +1536,7 @@ module ActiveRecord
"build_#{reflection.name}" => "build",
"create_#{reflection.name}" => "create"
}
+ constructors["create_#{reflection.name}!"] = "create!" if reflection.macro == :has_one
constructors.each do |name, proxy_name|
redefine_method(name) do |*params|
diff --git a/activerecord/lib/active_record/associations/has_one_association.rb b/activerecord/lib/active_record/associations/has_one_association.rb
index f60547c22c..c29ab8dcec 100644
--- a/activerecord/lib/active_record/associations/has_one_association.rb
+++ b/activerecord/lib/active_record/associations/has_one_association.rb
@@ -7,7 +7,7 @@ module ActiveRecord
end
def create!(attributes = {})
- new_record(:create_association!, attributes)
+ build(attributes).tap { |record| record.save! }
end
def build(attributes = {})
@@ -51,6 +51,9 @@ module ActiveRecord
alias creation_attributes construct_owner_attributes
+ # The reason that the save param for replace is false, if for create (not just build),
+ # is because the setting of the foreign keys is actually handled by the scoping, and
+ # so they are set straight away and do not need to be updated within replace.
def new_record(method, attributes)
record = scoped.scoping { @reflection.send(method, attributes) }
replace(record, false)