aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record
diff options
context:
space:
mode:
Diffstat (limited to 'activerecord/lib/active_record')
-rw-r--r--activerecord/lib/active_record/associations.rb11
-rw-r--r--activerecord/lib/active_record/associations/has_one_association.rb28
2 files changed, 11 insertions, 28 deletions
diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb
index e7d3e45da2..bbf96f52ed 100644
--- a/activerecord/lib/active_record/associations.rb
+++ b/activerecord/lib/active_record/associations.rb
@@ -1556,20 +1556,15 @@ module ActiveRecord
def association_constructor_method(constructor, reflection, association_proxy_class)
redefine_method("#{constructor}_#{reflection.name}") do |*params|
- attributees = params.first unless params.empty?
- replace_existing = params[1].nil? ? true : params[1]
- association = association_instance_get(reflection.name)
+ attributes = params.first unless params.empty?
+ association = association_instance_get(reflection.name)
unless association
association = association_proxy_class.new(self, reflection)
association_instance_set(reflection.name, association)
end
- if association_proxy_class == HasOneAssociation
- association.send(constructor, attributees, replace_existing)
- else
- association.send(constructor, attributees)
- end
+ association.send(constructor, attributes)
end
end
diff --git a/activerecord/lib/active_record/associations/has_one_association.rb b/activerecord/lib/active_record/associations/has_one_association.rb
index 5716bef524..f1e01197b5 100644
--- a/activerecord/lib/active_record/associations/has_one_association.rb
+++ b/activerecord/lib/active_record/associations/has_one_association.rb
@@ -4,22 +4,22 @@ module ActiveRecord
class HasOneAssociation < AssociationProxy #:nodoc:
include HasAssociation
- def create(attrs = {}, replace_existing = true)
- new_record(replace_existing) do |reflection|
+ def create(attrs = {})
+ new_record do |reflection|
attrs = merge_with_conditions(attrs)
reflection.create_association(attrs)
end
end
- def create!(attrs = {}, replace_existing = true)
- new_record(replace_existing) do |reflection|
+ def create!(attrs = {})
+ new_record do |reflection|
attrs = merge_with_conditions(attrs)
reflection.create_association!(attrs)
end
end
- def build(attrs = {}, replace_existing = true)
- new_record(replace_existing) do |reflection|
+ def build(attrs = {})
+ new_record do |reflection|
attrs = merge_with_conditions(attrs)
reflection.build_association(attrs)
end
@@ -82,21 +82,9 @@ module ActiveRecord
construct_owner_attributes
end
- def new_record(replace_existing)
- # Make sure we load the target first, if we plan on replacing the existing
- # instance. Otherwise, if the target has not previously been loaded
- # elsewhere, the instance we create will get orphaned.
- load_target if replace_existing
+ def new_record
record = scoped.scoping { yield @reflection }
-
- if replace_existing
- replace(record, true)
- else
- record[@reflection.foreign_key] = @owner.id if @owner.persisted?
- self.target = record
- set_inverse_instance(record)
- end
-
+ replace(record, true)
record
end