Add this line to your application's Gemfile:
gem "ksuid", github: "mattes/ksuid-ruby"To automatically use KSUID as primary key in migrations, create the following initializer:
# config/initializers/ksuid.rb
Rails.application.config.generators do |g|
g.orm :active_record, primary_key_type: :ksuid
endThis fork of michaelherold/ksuid-ruby has a couple of changes:
Previously each model had to be annotated with act_as_ksuid directives (see below). This fork adds native ksuid types
for Postgres and SQLite so no annotation is required.
For SQLite this works out of the box because of its dynamic typing.
For Postgres a custom data type is created.
Please note that MySQL does not support custom types, you're stuck with act_as_ksuid.
Old way:
class Event < ApplicationRecord
include KSUID::ActiveRecord[:foobar]
endNew way:
class Event < ApplicationRecord
act_as_ksuid :foobar
endPreviously act_as_ksuid and include KSUID::ActiveRecord[:foobar] both blindly initialized ksuid attributes with
a new KSUID. Now it only initializes a KSUID for primary keys. I believe this is the expected behavior, much like Postgres'
serial auto-increment - it only updates the primary key, not foreign key's for example.
If you marshal a model to JSON or YAML, to_json and to_yaml will convert KSUIDs into strings.