module Marten::DB::Model::Persistence

Direct including types

Defined in:

marten/db/model/persistence.cr

Instance Method Summary

Instance Method Detail

def delete(using : Nil | String | Symbol = nil) #

Deletes the model instance.

This methods deletes the model instance by complying to the deletion rules defined as part of the relation fields if applicable (on_delete option on many to one or one to one fields). It returns the number of rows that were deleted as part of the record deletion.


[View source]
def deleted? #

Returns a boolean indicating if the record was deleted or not.

This method returns true if the model instance was deleted previously. Otherwise it returns false in any other cases.


[View source]
def new_record? #

Returns a boolean indicating if the record doesn't exist in the database yet.

This methods returns true if the model instance hasn't been saved and doesn't exist in the database yet. In any other cases it returns false.


[View source]
def persisted? #

Returns a boolean indicating if the record is persisted in the database.

This method returns true if the record at hand exists in the database. Otherwise (if it's a new record or if it was deleted previously), the method returns false.


[View source]
def reload #

Reloads the model instance.

This methods retrieves the record at the database level and updates the current model instance with the new values.


[View source]
def save(using : Nil | String | Symbol = nil, validate : Bool = true) : Bool #

Saves the model instance.

If the model instance is new, a new record is created in the DB ; otherwise the existing record is updated. This method will return true if the model instance is valid and was created / updated successfully. Otherwise it will return false if the model instance validation failed.


[View source]
def save!(using : Nil | String | Symbol = nil, validate : Bool = true) : Bool #

Saves the model instance.

If the model instance is new, a new record is created in the DB ; otherwise the existing record is updated. This method will return true if the model instance is valid and was created / updated successfully. Otherwise it will raise a Marten::DB::Errors::InvalidRecord exception if the model instance validation failed.


[View source]
def update(values : Hash | NamedTuple) #

Updates the model instance.

This method updates the passed field values and then saves the record. This method returns true if the model instance is valid and was created / updated successfully. Otherwise it returns false if the model instance validation fails.


[View source]
def update(**values) #

Updates the model instance.

This method updates the passed field values and then saves the record. This method returns true if the model instance is valid and was created / updated successfully. Otherwise it returns false if the model instance validation fails.


[View source]
def update!(values : Hash | NamedTuple) #

Updates the model instance.

This method updates the passed field values and then saves the record. This method returns true if the model instance is valid and was created / updated successfully. Otherwise it raises a Marten::DB::Errors::InvalidRecord exception if the model instance validation fails.


[View source]
def update!(**values) #

Updates the model instance.

This method updates the passed field values and then saves the record. This method returns true if the model instance is valid and was created / updated successfully. Otherwise it raises a Marten::DB::Errors::InvalidRecord exception if the model instance validation fails.


[View source]