Callbacks
Call a specified method on a specific life cycle event.
Here is an example:
1
require "granite/adapter/pg"
2
3
class Post < Granite::Base
4
connection pg
5
6
before_save :upcase_title
7
8
column id : Int64, primary: true
9
column title : String
10
column content : String
11
timestamps
12
13
def upcase_title
14
if title = @title
15
@title = title.upcase
16
end
17
end
18
end
Copied!
You can register callbacks for the following events:

Create

    before_save
    before_create
    save
    after_create
    after_save

Update

    before_save
    before_update
    save
    after_update
    after_save

Destroy

    before_destroy
    destroy
    after_destroy
Last modified 2yr ago
Copy link