Skip to content

Instantly share code, notes, and snippets.

@dodafish
Created February 9, 2020 12:47
Show Gist options
  • Save dodafish/0b2a89ad476c6c4dab5000c5d818c955 to your computer and use it in GitHub Desktop.
Save dodafish/0b2a89ad476c6c4dab5000c5d818c955 to your computer and use it in GitHub Desktop.
UUIDs for Rails ActiveRecords
# Configures Rails to use UUIDs instead of incremental integers.
# Limits Rails to run with postgresql only (even in development and test environments)
# postgresql adapter
gem "pg"
# default to use uuids in generators
initializer 'generators.rb', <<-CODE
Rails.application.config.generators do |g|
g.orm :active_record, primary_key_type: :uuid
end
CODE
# enable extension in postgresql db
generate(:migration, 'EnableUUID')
@dodafish
Copy link
Author

dodafish commented Feb 9, 2020

enable_extension 'pgcrypto' unless extension_enabled?('pgcrypto')

Needs to be added to the EnableUUID migration to work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment