method
replace
v8.0.0 -
Show latest stable
- Class:
ActiveRecord::Associations::HasOneAssociation
replace(record, save = true)private
No documentation available.
# File activerecord/lib/active_record/associations/has_one_association.rb, line 59
def replace(record, save = true)
raise_on_type_mismatch!(record) if record
return target unless load_target || record
assigning_another_record = target != record
if assigning_another_record || record.has_changes_to_save?
save &&= owner.persisted?
transaction_if(save) do
remove_target!(options[:dependent]) if target && !target.destroyed? && assigning_another_record
if record
set_owner_attributes(record)
set_inverse_instance(record)
if save && !record.save
nullify_owner_attributes(record)
set_owner_attributes(target) if target
raise RecordNotSaved.new("Failed to save the new associated #{reflection.name}.", record)
end
end
end
end
self.target = record
end