diff options
Diffstat (limited to 'activerecord/lib/active_record/associations/singular_association.rb')
-rw-r--r-- | activerecord/lib/active_record/associations/singular_association.rb | 16 |
1 files changed, 10 insertions, 6 deletions
diff --git a/activerecord/lib/active_record/associations/singular_association.rb b/activerecord/lib/active_record/associations/singular_association.rb index b6f49c6f36..7f92d9712a 100644 --- a/activerecord/lib/active_record/associations/singular_association.rb +++ b/activerecord/lib/active_record/associations/singular_association.rb @@ -2,9 +2,7 @@ module ActiveRecord module Associations class SingularAssociation < AssociationProxy #:nodoc: def create(attributes = {}) - record = scoped.scoping { @reflection.create_association(attributes) } - set_new_record(record) - record + new_record(:create, attributes) end def create!(attributes = {}) @@ -12,12 +10,11 @@ module ActiveRecord end def build(attributes = {}) - record = scoped.scoping { @reflection.build_association(attributes) } - set_new_record(record) - record + new_record(:build, attributes) end private + def find_target scoped.first.tap { |record| set_inverse_instance(record) } end @@ -36,6 +33,13 @@ module ActiveRecord raise_on_type_mismatch(record) if record record end + + def new_record(method, attributes) + attributes = scoped.scope_for_create.merge(attributes || {}) + record = @reflection.send("#{method}_association", attributes) + set_new_record(record) + record + end end end end |