Module: DataMapper::Persistence::ConvenienceMethods::InstanceMethods

Defined in:
lib/data_mapper/persistence.rb

Instance Method Summary collapse

Instance Method Details

#destroy!Object

Deletes the model from the database and de-activates associations



184
185
186
# File 'lib/data_mapper/persistence.rb', line 184

def destroy!
  database_context.destroy(self)
end

#reload!(cols = nil) ⇒ Object Also known as: reload

Reloads a model’s properties from the database. This also includes data for any associated models that have been loaded from the database.

You can limit the properties being reloaded by passing in an array of symbols.



176
177
178
179
180
# File 'lib/data_mapper/persistence.rb', line 176

def reload!(cols = nil)
  database_context.first(self.class, key, :select => ([self.class.table.key.to_sym] + (cols || original_values.keys)).uniq, :reload => true)
  self.loaded_associations.each { |association| association.reload! }
  self
end

#saveObject

Save updated properties to the database.

Returns

True::  successfully saved the object to the database
False:: an error occured when saving the object to the database.
        check valid? to see if validation error occured


150
151
152
# File 'lib/data_mapper/persistence.rb', line 150

def save
  database_context.save(self)
end

#save!Object

This behaves in the same way as save, but raises a ValidationError if the model is invalid. Successful saves return true.

Returns

True:: successfully saved the object to the database

Raises

ValidationError::
   The object could not be saved to the database due to validation
   errors

Raises:



165
166
167
168
# File 'lib/data_mapper/persistence.rb', line 165

def save!
  raise ValidationError.new(errors) unless save
  return true
end