Module: Mongoid::Relations::AutoSave::ClassMethods

Defined in:
lib/mongoid/relations/auto_save.rb

Overview

:nodoc:

Instance Method Summary collapse

Instance Method Details

#autosave(metadata) ⇒ Object

Set up the autosave behaviour for references many and references one relations. When the option is set to true, these relations will get saved automatically when the parent is first saved, but not if the parent already exists in the database.

Examples:

Set up autosave options.

Person.autosave()

Parameters:

  • metadata (Metadata)

    The relation metadata.

Since:

  • 2.0.0.rc.1



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/mongoid/relations/auto_save.rb', line 23

def autosave()
  if .autosave?
    set_callback :save, :after do |document|
      relation = document.send(.name)
      if relation
        (relation.do_or_do_not(:in_memory) || relation.to_a).each do |doc|
          doc.save
        end
      end
    end
  end
end