module
PgORM::Timestamps
Overview
Automatically manages created_at and updated_at timestamp columns.
Include this module in your model to automatically track when records are created and updated. The timestamps are managed via callbacks:
created_atis set when the record is first savedupdated_atis set on every save (create and update)
Usage
class User < PgORM::Base
include PgORM::Timestamps
attribute id : Int64
attribute name : String
# created_at and updated_at are added automatically
end
user = User.create!(name: "John")
user.created_at # => 2024-01-15 10:30:00 UTC
user.updated_at # => 2024-01-15 10:30:00 UTC
sleep 5
user.update!(name: "Jane")
user.created_at # => 2024-01-15 10:30:00 UTC (unchanged)
user.updated_at # => 2024-01-15 10:30:05 UTC (updated)
Database Schema
Make sure your table has these columns:
CREATE TABLE users (
id BIGSERIAL PRIMARY KEY,
name TEXT NOT NULL,
created_at TIMESTAMP NOT NULL,
updated_at TIMESTAMP NOT NULL
);