From 52c47556b7cf55549f97f3cfd5f69b2563198eac Mon Sep 17 00:00:00 2001 From: Jon Leighton Date: Sun, 16 Jan 2011 18:25:50 +0000 Subject: Add create_association! for belongs_to --- activerecord/lib/active_record/associations.rb | 6 +++--- .../lib/active_record/associations/belongs_to_association.rb | 4 ++++ 2 files changed, 7 insertions(+), 3 deletions(-) (limited to 'activerecord/lib') 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 -- cgit v1.2.3