aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib
diff options
context:
space:
mode:
Diffstat (limited to 'activerecord/lib')
-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