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
# File 'lib/mongoid/relations/auto_save.rb', line 23

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