This blog is part of our Rails 5 series.

UUIDs are a popular alternative to auto-incremental integer primary keys.

create_table :users, id: :uuid do |t|
  t.string :name
end

Notice that id: :uuid is passed to create_table. This is all we need to do to have UUID as primary key for users.

Now, if an application is designed to use UUID instead of Integer, then chances are that new tables too would use UUID as primary key. And it can easily get repetitive to add id: :uuid in create_table , every time a new model is generated.

Rails 5 comes up with a solution. We need to set primary key as UUID in config/application.rb.

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

This automatically adds id: :uuid to create_table in all future migrations.

One small recurrence off our plate.