Module: ActiveRecord::AutosaveAssociation

Defined in:
lib/classy-inheritance.rb

Instance Method Summary collapse

Instance Method Details

#save_belongs_to_association(reflection) ⇒ Object

this is fixed in 2.3.3



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/classy-inheritance.rb', line 99

def save_belongs_to_association(reflection)
  if association = association_instance_get(reflection.name)
    autosave = reflection.options[:autosave]

    if autosave && association.marked_for_destruction?
      association.destroy
    else
      association.save(!autosave) if association.new_record? || autosave

      if association.updated?
        association_id = association.send(reflection.options[:primary_key] || :id)
        self[reflection.primary_key_name] = association_id
        # TODO: Removing this code doesn't seem to matter…
        if reflection.options[:polymorphic]
          self[reflection.options[:foreign_type]] = association.class.base_class.name.to_s
        end
      end
    end
  end
end

#save_has_one_association(reflection) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
# File 'lib/classy-inheritance.rb', line 86

def save_has_one_association(reflection)
  if (association = association_instance_get(reflection.name)) && !association.target.nil?
    primary_key = reflection.options[:primary_key] || :id  
    if reflection.options[:autosave] && association.marked_for_destruction?
      association.destroy
    elsif new_record? || association.new_record? || association[reflection.primary_key_name] != send(primary_key) || reflection.options[:autosave]
      association[reflection.primary_key_name] = send(primary_key)
      association.save(false)
    end
  end
end