aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/associations/singular_association.rb
diff options
context:
space:
mode:
Diffstat (limited to 'activerecord/lib/active_record/associations/singular_association.rb')
-rw-r--r--activerecord/lib/active_record/associations/singular_association.rb21
1 files changed, 11 insertions, 10 deletions
diff --git a/activerecord/lib/active_record/associations/singular_association.rb b/activerecord/lib/active_record/associations/singular_association.rb
index 877ddf3ee1..ce1f2a5543 100644
--- a/activerecord/lib/active_record/associations/singular_association.rb
+++ b/activerecord/lib/active_record/associations/singular_association.rb
@@ -17,27 +17,28 @@ module ActiveRecord
replace(record)
end
- def create(attributes = {}, options = {})
- build(attributes, options).tap { |record| record.save }
+ def create(attributes = {}, options = {}, &block)
+ build(attributes, options, &block).tap { |record| record.save }
end
- def create!(attributes = {}, options = {})
- build(attributes, options).tap { |record| record.save! }
+ def create!(attributes = {}, options = {}, &block)
+ build(attributes, options, &block).tap { |record| record.save! }
end
def build(attributes = {}, options = {})
- record = reflection.build_association
- record.assign_attributes(
- scoped.scope_for_create.except(klass.primary_key),
- :without_protection => true
- )
- record.assign_attributes(attributes, options)
+ record = reflection.build_association(attributes, options)
+ record.assign_attributes(create_scope.except(*record.changed), :without_protection => true)
+ yield(record) if block_given?
set_new_record(record)
record
end
private
+ def create_scope
+ scoped.scope_for_create.stringify_keys.except(klass.primary_key)
+ end
+
def find_target
scoped.first.tap { |record| set_inverse_instance(record) }
end