Module: TrackBallast::UuidManagement
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/track_ballast/uuid_management.rb
Overview
Manage a uuid
column alongside an autoincrementing id
column for an ActiveRecord
model.
Additional Features
-
Adds presence validation if the
uuid
column is non-nullable -
Validates v4 UUIDs at creation time using the
not_v4_uuid
error onuuid
and logs violations with ainvalid-uuid
tag
Usage
Adding a Column
Add a uuid
column to your model such that it can be written as a string.
Suggested:
t.string "uuid", limit: 36, null: false, unique: true
Alternatively, for MySQL, consider using a binary column:
t.binary "uuid", limit: 16, null: false, unique: true
…and define the uuid
attribute using the mysql-binuuid-rails
gem:
attribute :uuid, MySQLBinUUID::Type.new
This has performance and storage space benefits, but please note that this may increase the difficulty of working with this column outside of Rails.
Both forms of UUID column are acceptable and left as a decision for the implementor.
Include the module
After adding the column, simply include
the module:
class MyModel < ApplicationRecord
include TrackBallast::UuidManagement
end