module Clear::Model::HasSaving

Direct including types

Defined in:

clear/model/modules/has_saving.cr

Instance Method Summary

Instance Method Detail

def delete #

Delete the model by building and executing a DELETE query. A deleted model is not persisted anymore, and can be saved again. Clear will do INSERT instead of UPDATE then Return true if the model has been successfully deleted, and false otherwise.


[View source]
def persisted? : Bool #

[View source]
def reload : self #

[View source]
def save(on_conflict : Clear::SQL::InsertQuery -> | Nil = nil) #

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@example.com"
u.save(-> (qry) { qry.on_conflict.do_update { |u| u.set(email: "email@example.com") } #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@example.com"
u.save do |qry|
   qry.on_conflict.do_update { |u| u.set(email: "email@example.com")
end

[View source]
def save(&block) #

[View source]
def save!(on_conflict : Clear::SQL::InsertQuery -> | Nil = nil) #

Performs #save call, but instead of returning false if validation failed, raise Clear::Model::InvalidError exception


[View source]
def save!(&block : Clear::SQL::InsertQuery -> ) #

Pass the on_conflict optional parameter via block.


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

Set the fields passed as argument and call #save on the object


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

Set the fields passed as argument and call #save! on the object


[View source]