Call a specified method on a specific life cycle event.
Here is an example:
require "granite/adapter/pg"
class Post < Granite::Base
connection pg
before_save :upcase_title
column id : Int64, primary: true
column title : String
column content : String
timestamps
def upcase_title
if title = @title
@title = title.upcase
end
end
end
You can register callbacks for the following events:
- before_save
- before_create
- save
- after_create
- after_save
- before_save
- before_update
- save
- after_update
- after_save
- before_destroy
- destroy
- after_destroy