module
Marten::DB::Model::Persistence
Direct including types
Defined in:
marten/db/model/persistence.crInstance Method Summary
-
#delete(using : Nil | String | Symbol = nil)
Deletes the model instance.
-
#deleted?
Returns a boolean indicating if the record was deleted or not.
-
#new_record?
Returns a boolean indicating if the record doesn't exist in the database yet.
-
#persisted?
Returns a boolean indicating if the record is persisted in the database.
-
#reload
Reloads the model instance.
-
#save(using : Nil | String | Symbol = nil, validate : Bool = true) : Bool
Saves the model instance.
-
#save!(using : Nil | String | Symbol = nil, validate : Bool = true) : Bool
Saves the model instance.
-
#update(values : Hash | NamedTuple)
Updates the model instance.
-
#update(**values)
Updates the model instance.
-
#update!(values : Hash | NamedTuple)
Updates the model instance.
-
#update!(**values)
Updates the model instance.
-
#update_columns(values : Hash | NamedTuple) : Bool
Updates specific columns in the database without running validations or callbacks.
-
#update_columns(**values) : Bool
Updates specific columns in the database without running validations or callbacks.
-
#update_columns!(values : Hash | NamedTuple) : Bool
Updates specific columns in the database without running validations or callbacks.
-
#update_columns!(**values) : Bool
Updates specific columns in the database without running validations or callbacks.
Instance Method Detail
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.
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.
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.
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.
Reloads the model instance.
This methods retrieves the record at the database level and updates the current model instance with the new values.
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.
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.
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.
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.
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.
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.
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
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
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
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