Class: ActiveRecord::Associations::HasOneAssociation
- Inherits:
-
AssociationProxy
- Object
- AssociationProxy
- ActiveRecord::Associations::HasOneAssociation
- Defined in:
- activerecord/lib/active_record/associations/has_one_association.rb
Overview
:nodoc:
Instance Method Summary (collapse)
- - (Object) build(attrs = {}, replace_existing = true)
- - (Object) create(attrs = {}, replace_existing = true)
- - (Object) create!(attrs = {}, replace_existing = true)
-
- (HasOneAssociation) initialize(owner, reflection)
constructor
A new instance of HasOneAssociation.
- - (Object) replace(obj, dont_save = false)
Methods inherited from AssociationProxy
#===, #aliased_table_name, #conditions, #flatten_deeper, #inspect, #loaded, #loaded?, #proxy_owner, #proxy_reflection, #proxy_target, #reload, #reset, #respond_to?, #send, #target, #target=
Constructor Details
- (HasOneAssociation) initialize(owner, reflection)
A new instance of HasOneAssociation
5 6 7 8 |
# File 'activerecord/lib/active_record/associations/has_one_association.rb', line 5 def initialize(owner, reflection) super construct_sql end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class ActiveRecord::Associations::AssociationProxy
Instance Method Details
- (Object) build(attrs = {}, replace_existing = true)
24 25 26 27 28 29 |
# File 'activerecord/lib/active_record/associations/has_one_association.rb', line 24 def build(attrs = {}, replace_existing = true) new_record(replace_existing) do |reflection| attrs = merge_with_conditions(attrs) reflection.build_association(attrs) end end |
- (Object) create(attrs = {}, replace_existing = true)
10 11 12 13 14 15 |
# File 'activerecord/lib/active_record/associations/has_one_association.rb', line 10 def create(attrs = {}, replace_existing = true) new_record(replace_existing) do |reflection| attrs = merge_with_conditions(attrs) reflection.create_association(attrs) end end |
- (Object) create!(attrs = {}, replace_existing = true)
17 18 19 20 21 22 |
# File 'activerecord/lib/active_record/associations/has_one_association.rb', line 17 def create!(attrs = {}, replace_existing = true) new_record(replace_existing) do |reflection| attrs = merge_with_conditions(attrs) reflection.create_association!(attrs) end end |
- (Object) replace(obj, dont_save = false)
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'activerecord/lib/active_record/associations/has_one_association.rb', line 31 def replace(obj, dont_save = false) load_target unless @target.nil? || @target == obj if dependent? && !dont_save case @reflection.[:dependent] when :delete @target.delete if @target.persisted? @owner.clear_association_cache when :destroy @target.destroy if @target.persisted? @owner.clear_association_cache when :nullify @target[@reflection.primary_key_name] = nil @target.save if @owner.persisted? && @target.persisted? end else @target[@reflection.primary_key_name] = nil @target.save if @owner.persisted? && @target.persisted? 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.persisted? or obj.nil? or dont_save return (obj.save ? self : false) else return (obj.nil? ? nil : self) end end |