aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record
diff options
context:
space:
mode:
authorJon Leighton <j@jonathanleighton.com>2011-01-16 18:25:50 +0000
committerAaron Patterson <aaron.patterson@gmail.com>2011-01-16 13:43:53 -0800
commit52c47556b7cf55549f97f3cfd5f69b2563198eac (patch)
treec43cfba16b8e7c0944d42bbc4224b9a178ec7a7f /activerecord/lib/active_record
parent8f88a2841682d95c7bcce7ee76c156ec24bf82d8 (diff)
downloadrails-52c47556b7cf55549f97f3cfd5f69b2563198eac.tar.gz
rails-52c47556b7cf55549f97f3cfd5f69b2563198eac.tar.bz2
rails-52c47556b7cf55549f97f3cfd5f69b2563198eac.zip
Add create_association! for belongs_to
Diffstat (limited to 'activerecord/lib/active_record')
-rw-r--r--activerecord/lib/active_record/associations.rb6
-rw-r--r--activerecord/lib/active_record/associations/belongs_to_association.rb4
2 files changed, 7 insertions, 3 deletions
diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb
index a03d1bbb06..70c8e75e20 100644
--- a/activerecord/lib/active_record/associations.rb
+++ b/activerecord/lib/active_record/associations.rb
@@ -1533,10 +1533,10 @@ module ActiveRecord
def association_constructor_methods(reflection)
constructors = {
- "build_#{reflection.name}" => "build",
- "create_#{reflection.name}" => "create"
+ "build_#{reflection.name}" => "build",
+ "create_#{reflection.name}" => "create",
+ "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/belongs_to_association.rb b/activerecord/lib/active_record/associations/belongs_to_association.rb
index b5545f4084..d311b0c572 100644
--- a/activerecord/lib/active_record/associations/belongs_to_association.rb
+++ b/activerecord/lib/active_record/associations/belongs_to_association.rb
@@ -6,6 +6,10 @@ module ActiveRecord
replace(@reflection.create_association(attributes))
end
+ def create!(attributes = {})
+ build(attributes).tap { |record| record.save! }
+ end
+
def build(attributes = {})
replace(@reflection.build_association(attributes))
end