Included Modules

Class/Module Index [+]

Quicksearch

Mongoid::Persistence

The persistence module is a mixin to provide database accessor methods for the document. These correspond to the appropriate accessors on a mongo collection and retain the same DSL.

@example Sample persistence operations.

document.insert
document.update
document.upsert

Public Instance Methods

delete(options = {}) click to toggle source
Alias for: remove
destroy(options = {}) click to toggle source

Remove the document from the datbase with callbacks.

@example Destroy a document.

document.destroy

@param [ Hash ] options Options to pass to destroy.

@return [ true, false ] True if successful, false if not.

# File lib/mongoid/persistence.rb, line 29
def destroy(options = {})
  run_callbacks(:destroy) { remove(options) }
end
insert(options = {}) click to toggle source

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

@example Insert a document.

document.insert

@param [ Hash ] options Options to pass to insert.

@return [ Document ] The persisted document.

# File lib/mongoid/persistence.rb, line 42
def insert(options = {})
  Operations.insert(self, options).persist
end
remove(options = {}) click to toggle source

Remove the document from the datbase.

@example Remove the document.

document.remove

@param [ Hash ] options Options to pass to remove.

@return [ TrueClass ] True.

# File lib/mongoid/persistence.rb, line 54
def remove(options = {})
  Operations.remove(self, options).persist
end
Also aliased as: delete
save(options = {}) click to toggle source
Alias for: upsert
save!(options = {}) click to toggle source

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.

@example Save the document.

document.save!

@param [ Hash ] options Options to pass to the save.

@return [ true, false ] True if validation passed.

# File lib/mongoid/persistence.rb, line 68
def save!(options = {})
  unless upsert(options)
    self.class.fail_validate!(self) if errors.any?
    self.class.fail_callback!(self, :save!)
  end
  return true
end
update(options = {}) click to toggle source

Update the document in the datbase.

@example Update an existing document.

document.update

@param [ Hash ] options Options to pass to update.

@return [ true, false ] True if succeeded, false if not.

# File lib/mongoid/persistence.rb, line 84
def update(options = {})
  Operations.update(self, options).persist
end
update_attribute(name, value) click to toggle source

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

@example Update the attribute.

person.update_attribute(:title, "Sir")

@param [ Symbol, String ] name The name of the attribute. @param [ Object ] value The new value of the attribute.a

@return [ true, false ] True if save was successfull, false if not.

@since 2.0.0.rc.6

# File lib/mongoid/persistence.rb, line 100
def update_attribute(name, value)
  write_attribute(name, value)
  save(:validate => false)
end
update_attributes(attributes = {}) click to toggle source

Update the document attributes in the datbase.

@example Update the document’s attributes

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

@param [ Hash ] attributes The attributes to update.

@return [ true, false ] True if validation passed, false if not.

# File lib/mongoid/persistence.rb, line 113
def update_attributes(attributes = {})
  write_attributes(attributes); save
end
update_attributes!(attributes = {}) click to toggle source

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

@example Update the document’s attributes.

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

@param [ Hash ] attributes The attributes to update.

@raise [ Errors::Validations ] If validation failed.

@return [ true, false ] True if validation passed.

# File lib/mongoid/persistence.rb, line 128
def update_attributes!(attributes = {})
  update_attributes(attributes).tap do |result|
    unless result
      self.class.fail_validate!(self) if errors.any?
      self.class.fail_callback!(self, :update_attributes!)
    end
  end
end
upsert(options = {}) click to toggle source

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

@example Upsert the document.

document.upsert

@param [ Hash ] options Options to pass to the upsert.

@return [ true, false ] True is success, false if not.

# File lib/mongoid/persistence.rb, line 146
def upsert(options = {})
  if new_record?
    insert(options).persisted?
  else
    update(options)
  end
end
Also aliased as: save

[Validate]

Generated with the Darkfish Rdoc Generator 2.