Today I Learned

hashrocket A Hashrocket project

Add a UUID Datatype to Your Rails App

Create a migration to add the extension


class CreateUuidExtension < ActiveRecord::Migration
  def change
    create_table :uuid_extensions do |t|
      enable_extension 'uuid-ossp'
    end
  end
end

Add a column to your table with a data type of :uuid. Don't forget to add a default of uuid_generate_v4()

class AddUuidToAccounts < ActiveRecord::Migration
  def change
    add_column :accounts, :uuid, :uuid, default: 'uuid_generate_v4()'
  end
end
See More #rails TILs
Looking for help? Hashrocket has been an industry leader in Ruby on Rails since 2008. Rails is a core skill for each developer at Hashrocket, and we'd love to take a look at your project. Contact us and find out how we can help you.