method
replace
v2.2.1 -
Show latest stable
- Class:
ActiveRecord::Associations::HasOneAssociation
replace(obj, dont_save = false)public
No documentation available.
# File activerecord/lib/active_record/associations/has_one_association.rb, line 27
def replace(obj, dont_save = false)
load_target
unless @target.nil? || @target == obj
if dependent? && !dont_save
@target.destroy unless @target.new_record?
@owner.clear_association_cache
else
@target[@reflection.primary_key_name] = nil
@target.save unless @owner.new_record? || @target.new_record?
end
end
if obj.nil?
@target = nil
else
raise_on_type_mismatch(obj)
set_belongs_to_association_for(obj)
@target = (AssociationProxy === obj ? obj.target : obj)
end
@loaded = true
unless @owner.new_record? or obj.nil? or dont_save
return (obj.save ? self : false)
else
return (obj.nil? ? nil : self)
end
end