-
Notifications
You must be signed in to change notification settings - Fork 456
Usage with UUID's
Rinas edited this page Jul 12, 2023
·
9 revisions
For versions 3.4+
When using ancestry with a uuid primary key column, the validation of the ancestry column needs to be updated.
By default, it assumes the id will be integers only, so the primary_key_format
option changes the validation to best meet your needs.
# app/models/model.rb
class Model < ActiveRecord::Base
has_ancestry primary_key_format: '[\w-]{36}'
end
Or if all ancestry columns use uuids, you can set the default primary key format in an initializer:
# config/initializer/ancestry.rb
Ancestry.default_primary_key_format = '[\w-]{36}
# app/models/model.rb
class Model < ActiveRecord::Base
has_ancestry
end
Of course it depends on how your uuid's are shaped, this regex matches ids made up of: a-zA-Z0-9_-
.
The Regex for RFC 4122 UUID is /[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}/i
(matches versions 1 through 5, based on this SO post).