Module: Mongoid::Persistence

Extended by:
ActiveSupport::Concern
Includes:
Atomic
Included in:
Components
Defined in:
lib/mongoid/persistence.rb,
lib/mongoid/persistence/atomic.rb,
lib/mongoid/persistence/insert.rb,
lib/mongoid/persistence/remove.rb,
lib/mongoid/persistence/update.rb,
lib/mongoid/persistence/command.rb,
lib/mongoid/persistence/atomic/inc.rb,
lib/mongoid/persistence/remove_all.rb,
lib/mongoid/persistence/atomic/push.rb,
lib/mongoid/persistence/atomic/pull_all.rb,
lib/mongoid/persistence/insert_embedded.rb,
lib/mongoid/persistence/remove_embedded.rb,
lib/mongoid/persistence/atomic/operation.rb,
lib/mongoid/persistence/atomic/add_to_set.rb

Overview

:nodoc:

Defined Under Namespace

Modules: Atomic, ClassMethods Classes: Command, Insert, InsertEmbedded, Remove, RemoveAll, RemoveEmbedded, Update

Instance Method Summary collapse

Methods included from Atomic

#add_to_set, #inc, #pull_all, #push

Instance Method Details

#destroy(options = {}) ⇒ true, false

Remove the document from the datbase with callbacks.

Examples:

Destroy a document.

document.destroy

Parameters:

  • options (Hash) (defaults to: {})

    Options to pass to destroy.

Returns:

  • (true, false)

    True if successful, false if not.



33
34
35
# File 'lib/mongoid/persistence.rb', line 33

def destroy(options = {})
  run_callbacks(:destroy) { remove(options) }
end

#insert(options = {}) ⇒ Document

Insert a new document into the database. Will return the document itself whether or not the save was successful.

Examples:

Insert a document.

document.insert

Parameters:

  • options (Hash) (defaults to: {})

    Options to pass to insert.

Returns:

  • (Document)

    The persisted document.



46
47
48
# File 'lib/mongoid/persistence.rb', line 46

def insert(options = {})
  Insert.new(self, options).persist
end

#remove(options = {}) ⇒ TrueClass Also known as: delete

Remove the document from the datbase.

Examples:

Remove the document.

document.remove

Parameters:

  • options (Hash) (defaults to: {})

    Options to pass to remove.

Returns:



58
59
60
61
62
63
64
# File 'lib/mongoid/persistence.rb', line 58

def remove(options = {})
  if Remove.new(self, options).persist
    attributes.freeze
    self.destroyed = true
    cascade!
  end; true
end

#save!(options = {}) ⇒ true, false

Save the document - will perform an insert if the document is new, and update if not. If a validation error occurs an error will get raised.

Examples:

Save the document.

document.save!

Parameters:

  • options (Hash) (defaults to: {})

    Options to pass to the save.

Returns:

  • (true, false)

    True if validation passed.



76
77
78
# File 'lib/mongoid/persistence.rb', line 76

def save!(options = {})
  self.class.fail_validate!(self) unless upsert(options); true
end

#update(options = {}) ⇒ true, false

Update the document in the datbase.

Examples:

Update an existing document.

document.update

Parameters:

  • options (Hash) (defaults to: {})

    Options to pass to update.

Returns:

  • (true, false)

    True if succeeded, false if not.



88
89
90
# File 'lib/mongoid/persistence.rb', line 88

def update(options = {})
  Update.new(self, options).persist
end

#update_attribute(name, value) ⇒ true, false

Update a single attribute and persist the entire document. This skips validation but fires the callbacks.

Examples:

Update the attribute.

person.update_attribute(:title, "Sir")

Parameters:

  • name (Symbol, String)

    The name of the attribute.

  • value (Object)

    The new value of the attribute.a

Returns:

  • (true, false)

    True if save was successfull, false if not.

Since:

  • 2.0.0.rc.6



104
105
106
# File 'lib/mongoid/persistence.rb', line 104

def update_attribute(name, value)
  write_attribute(name, value) ? save(:validate => false) : true
end

#update_attributes(attributes = {}) ⇒ true, false

Update the document attributes in the datbase.

Examples:

Update the document’s attributes

document.update_attributes(:title => "Sir")

Parameters:

  • attributes (Hash) (defaults to: {})

    The attributes to update.

Returns:

  • (true, false)

    True if validation passed, false if not.



116
117
118
# File 'lib/mongoid/persistence.rb', line 116

def update_attributes(attributes = {})
  write_attributes(attributes); save
end

#update_attributes!(attributes = {}) ⇒ true, false

Update the document attributes in the database and raise an error if validation failed.

Examples:

Update the document’s attributes.

document.update_attributes(:title => "Sir")

Parameters:

  • attributes (Hash) (defaults to: {})

    The attributes to update.

Returns:

  • (true, false)

    True if validation passed.

Raises:



131
132
133
134
135
# File 'lib/mongoid/persistence.rb', line 131

def update_attributes!(attributes = {})
  update_attributes(attributes).tap do |result|
    self.class.fail_validate!(self) unless result
  end
end

#upsert(options = {}) ⇒ true, false Also known as: save

Upsert the document - will perform an insert if the document is new, and update if not.

Examples:

Upsert the document.

document.upsert

Parameters:

  • options (Hash) (defaults to: {})

    Options to pass to the upsert.

Returns:

  • (true, false)

    True is success, false if not.



146
147
148
149
150
151
152
# File 'lib/mongoid/persistence.rb', line 146

def upsert(options = {})
  if new_record?
    insert(options).persisted?
  else
    update(options)
  end
end