module
Lustra::Model::HasSaving
Overview
This module handles saving models to the database and triggers lifecycle callbacks.
It's where the actual :create
, :update
, :delete
, and :save
callbacks are triggered.
Key callback trigger points:
:save
callbacks: triggered for the entire save operation (wraps create/update):create
callbacks: triggered when a new record is inserted into the database:update
callbacks: triggered when an existing record is updated in the database:delete
callbacks: triggered when a record is deleted from the database
The callbacks are triggered via with_triggers
which calls Lustra::Model::EventManager
Direct including types
Defined in:
lustra/model/modules/has_saving.crInstance Method Summary
-
#delete
Delete the model by building and executing a
DELETE
query. - #persisted? : Bool
- #reload : self
-
#save(on_conflict : Lustra::SQL::InsertQuery -> | Nil = nil)
Save the model.
- #save(&block)
-
#save!(on_conflict : Lustra::SQL::InsertQuery -> | Nil = nil)
Performs
#save
call, but instead of returningfalse
if validation failed, raiseLustra::Model::InvalidError
exception Automatically handles built associations -
#save!(&block : Lustra::SQL::InsertQuery -> )
Pass the
on_conflict
optional parameter via block. -
#save_with_associations(on_conflict : Lustra::SQL::InsertQuery -> | Nil = nil)
Save the model along with all built associations
-
#update(**args)
Set the fields passed as argument and call
#save
on the object -
#update!(**args)
Set the fields passed as argument and call
#save!
on the object
Instance Method Detail
Delete the model by building and executing a DELETE
query.
A deleted model is not persisted anymore, and can be saved again.
Lustra will do INSERT
instead of UPDATE
then
Return true
if the model has been successfully deleted, and false
otherwise.
Save the model. If the model is already persisted, will call UPDATE
query.
If the model is not persisted, will call INSERT
Optionally, you can pass a Proc
to refine the INSERT
with on conflict
resolution functions.
Return false
if the model cannot be saved (validation issue)
Return true
if the model has been correctly saved.
Example:
u = User.new
if u.save
puts "User correctly saved !"
else
puts "There was a problem during save: "
# do something with `u.errors`
end
on_conflict
optional parameter
Example:
u = User.new id: 123, email: "[email protected]"
u.save(-> (qry) { qry.on_conflict.do_update { |u| u.set(email: "[email protected]") } #update
# IMPORTANT NOTICE: user may not be saved, but will be still detected as persisted !
You may want to use a block for on_conflict
optional parameter:
u = User.new id: 123, email: "[email protected]"
u.save do |qry|
qry.on_conflict.do_update { |u| u.set(email: "[email protected]")
end
Performs #save
call, but instead of returning false
if validation failed,
raise Lustra::Model::InvalidError
exception
Automatically handles built associations
Pass the on_conflict
optional parameter via block.
Save the model along with all built associations