method
replace
v2.3.8 -
Show latest stable
-
0 notes -
Class: ActiveRecord::Associations::HasOneAssociation
- 1.0.0
- 1.1.6
- 1.2.6
- 2.0.3
- 2.1.0 (0)
- 2.2.1 (0)
- 2.3.8 (0)
- 3.0.0 (0)
- 3.0.9 (0)
- 3.1.0 (0)
- 3.2.1 (0)
- 3.2.8 (0)
- 3.2.13 (0)
- 4.0.2 (0)
- 4.1.8 (0)
- 4.2.1 (0)
- 4.2.7 (0)
- 4.2.9 (0)
- 5.0.0.1 (0)
- 5.1.7 (0)
- 5.2.3 (0)
- 6.0.0 (0)
- 6.1.3.1 (0)
- 6.1.7.7 (0)
- 7.0.0 (0)
- 7.1.3.2 (0)
- 7.1.3.4 (0)
- 7.2.3 (0)
- 8.0.0 (0)
- 8.1.1 (0)
- What's this?
replace(obj, dont_save = false)
public
Hide source
# File activerecord/lib/active_record/associations/has_one_association.rb, line 30 def replace(obj, dont_save = false) load_target unless @target.nil? || @target == obj if dependent? && !dont_save case @reflection.options[:dependent] when :delete @target.delete unless @target.new_record? @owner.clear_association_cache when :destroy @target.destroy unless @target.new_record? @owner.clear_association_cache when :nullify @target[@reflection.primary_key_name] = nil @target.save unless @owner.new_record? || @target.new_record? end 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 set_inverse_instance(obj, @owner) @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

