Ruby on Rails
A universally unique identifier (UUID) is a 128-bit number used to identify information in computer systems. Sometimes it is referred to as a 'globally unique identifier'.
Using UUID instead of a simple ID in your Rials application has the following benefits:
If you have decided to use the UUID in your Rails application beware of the following drawbacks:
ModelName.first ModelName.last ModelName.second ModelName.second_to_last
As we have briefly discussed the benefits and drawbacks of using UUID lets jump into How we can set up our Rails application to use UUID instead of regular incremental IDs
To enable UUID in PostgreSQL we need to create a migration
rails g migration enable_uuid_support
Add the following code in the generated migration file
# config/db/migrate/enable_uuid_support.rb
class EnableUuidSupport < ActiveRecord::Migration[6.0]
def change
enable_extension 'pgcrypto'
end
end
Now set UUID as the default primary key in the generators.rb file as follows
# config/initializers/generators.rb
Rails.application.config.generators do |g|
g.orm :active_record, primary_key_type: :uuid
end
Everything is set, now it’s time to create your first model using UUID.
rails g model user name:string
And tell active record to use UUID as id with the following parameter id: :uuid
# config/db/migrate/create_user.rb
class CreateUsers < ActiveRecord::Migration[6.0]
def change
create_table :users, id: :uuid do |t|
t.string :name
t.timestamps
end
end
end
NOTE: You also need to set the type as UUID to every reference in your model see the below migration for the article model (article belongs to the user)
rails g model article
Generated migration’s code
# config/db/migrate/create_post_with_uuid.rb
class CreateArticle < ActiveRecord::Migration[6.0]
def change
create_table :users, id: :uuid do |t|
t.string :title
t.text :body
# Note you have to manually specify the type now
t.references :user, type: :uuid
t.timestamps
end
end
end
That’s all! Happy coding!
Rails is most suitable framework to build your startup.
Rails is a go-to web framework when you want to create a complex web application. But when it comes to APIs there are many players like Roda, Sinatra, Padrino, Grape and, Rails API.
React with Rails: Tutorial provides a complete understanding of using React JS in Ruby on Rails web application.
We value your privacy
We use cookies and other tracking technologies to enhance your experience on our website. We may store and/or access information on a device and process personal data, such as your IP address and browsing data, for personalized advertising and content, advertising and content measurement, audience research, and services development. Additionally, we may use precise geolocation data and identification through device scanning. Please note that your consent will be valid across all our subdomains. You can change or withdraw your consent at any time. We respect your choices and are committed to providing you with a transparent and secure browsing experience.