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]
def update_columns(values : Hash | NamedTuple) : Bool #

Updates specific columns in the database without running validations or callbacks.

This method allows you to update only the specified columns while leaving other fields unchanged. Unlike #update, this method bypasses model validations and lifecycle callbacks (such as before_update, after_update, etc.), making it more efficient for partial updates where validations and callbacks are not required.

Both the in-memory model instance and the database record are updated. However, this method does not reload the record after the update, so any changes made to other fields by database triggers or defaults will not be reflected in the model instance.

user = User.get!(id: 42)
user.update_columns(last_login: Time.utc)                    # Updates only last_login
user.update_columns(username: "jd", email: "[email protected]") # Updates multiple columns

[View source]
def update_columns(**values) : Bool #

Updates specific columns in the database without running validations or callbacks.

This method allows you to update only the specified columns while leaving other fields unchanged. Unlike #update, this method bypasses model validations and lifecycle callbacks (such as before_update, after_update, etc.), making it more efficient for partial updates where validations and callbacks are not required.

Both the in-memory model instance and the database record are updated. However, this method does not reload the record after the update, so any changes made to other fields by database triggers or defaults will not be reflected in the model instance.

user = User.get!(id: 42)
user.update_columns(last_login: Time.utc)                    # Updates only last_login
user.update_columns(username: "jd", email: "[email protected]") # Updates multiple columns

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

Updates specific columns in the database without running validations or callbacks.

This method provides the same functionality as #update_columns but with stricter validation. It raises a Marten::DB::Errors::UnmetSaveCondition exception if called on a new (unsaved) record, ensuring that updates are only performed on persisted records.

Like #update_columns, this method bypasses model validations and lifecycle callbacks, making it suitable for performance-critical updates where these features are not needed.

user = User.get!(id: 42)
user.update_columns!(last_login: Time.utc) # Updates only last_login

new_user = User.new(username: "jd")
new_user.update_columns!(email: "[email protected]") # Raises UnmetSaveCondition

[View source]
def update_columns!(**values) : Bool #

Updates specific columns in the database without running validations or callbacks.

This method provides the same functionality as #update_columns but with stricter validation. It raises a Marten::DB::Errors::UnmetSaveCondition exception if called on a new (unsaved) record, ensuring that updates are only performed on persisted records.

Like #update_columns, this method bypasses model validations and lifecycle callbacks, making it suitable for performance-critical updates where these features are not needed.

user = User.get!(id: 42)
user.update_columns!(last_login: Time.utc) # Updates only last_login

new_user = User.new(username: "jd")
new_user.update_columns!(email: "[email protected]") # Raises UnmetSaveCondition

[View source]