Module: Mongoid::Commands::InstanceMethods

Defined in:
lib/mongoid/commands.rb

Instance Method Summary collapse

Instance Method Details

#deleteObject

Delete the Document from the database. Delegates to the Delete command.



39
40
41
# File 'lib/mongoid/commands.rb', line 39

def delete
  Delete.execute(self)
end

#destroyObject

Destroy the Document. Delegates to the Destroy command.



44
45
46
# File 'lib/mongoid/commands.rb', line 44

def destroy
  Destroy.execute(self)
end

#saveObject

Save the Document. Delegates to the Save command.



49
50
51
# File 'lib/mongoid/commands.rb', line 49

def save
  new_record? ? Create.execute(self) : Save.execute(self)
end

#save!Object

Save the Document. Delegates to the Save command. If the command returns false then a ValidationError will be raised.



55
56
57
58
59
60
61
# File 'lib/mongoid/commands.rb', line 55

def save!
  if new_record?
    return Create.execute(self) || (raise Errors::Validations.new(self.errors))
  else
    return Save.execute(self) || (raise Errors::Validations.new(self.errors))
  end
end

#update_attributes(attrs = {}) ⇒ Object

Update the attributes of the Document. Will call save after the attributes have been updated.



65
66
67
# File 'lib/mongoid/commands.rb', line 65

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

#update_attributes!(attrs = {}) ⇒ Object

Update the attributes of the Document. Will call save! after the attributes have been updated, causing a ValidationError if the Document failed validation.



72
73
74
# File 'lib/mongoid/commands.rb', line 72

def update_attributes!(attrs = {})
  write_attributes(attrs); save!
end