Using UUIDs as primary keys in Rails apps

If you are using UUIDs as primary keys in your Rails application, you may find it tedious to specify them in every single migration. Fortunately, Rails provides a clean way to set this configuration globally so that all new tables use UUIDs automatically.

By adding a simple generator configuration to your config/application.rb, Rails will handle the primary key type for you. Inside your application class, define:

config.generators do |g|
  g.orm :active_record, primary_key_type: :uuid
end

With this setting in place, any time you generate a new model, Rails will create migrations that use UUIDs as the default primary key. This eliminates repetitive setup and ensures consistency across your database schema. It’s a small change that can save you time!