[RUBY] Introduction and precautions of gem Friendly Id

About gem Friendly Id

Since there is FriendlyId Guide, I will translate a part of the excerpt.

FriendlyId is a Ruby Active Record add-on that allows you to replace the id in a URL with a string.

** If you don't use FriendlyId ** http://example.com/states/4323454 with FriendlyId http://example.com/states/washington Create a part of the URL that identifies the page using human-readable keywords. This can make your application more friendly to both users and search engines.

You can see that the origin of the gem name FriendlyId comes from here.

Implementation example

Our service Carely actually uses friendly_id, so I would like to introduce it.

class Customer < ApplicationRecord
  include FriendlyId ---- ①

  friendly_id :uuid, use: :finders ---- ②

(1) To use FriendlyId in model, you need to include FriendlyId module first.

(2) This is a description that enables you to use the find method of active record with uuid.

customer: {
  id: 10,
  uuid: "28d0d2b8-3f2c-49d0-bf11-3b01ec164311"
  firstName: "hogehoge",
  lastName: "fugafuga", 
  mailAddress: "[email protected]",
  age": "30"
・
・
・
}

Suppose you have csutomer data like the one above.

Originally find only supports the primary key (id) customer = Customer.find(10) It will be described as friendly_id :uuid, use: :finders By writing customer = Customer.find("28d0d2b8-3f2c-49d0-bf11-3b01ec164311") You can also use the find method with and uuid. Also, when displaying the information of the corresponding customer for the URL http://example.com/customer/10 It was, but by using FriendlyId http://example.com/customer/28d0d2b8-3f2c-49d0-bf11-3b01ec164311 It will be. The benefits of using FriendlyId are as described in the FriendlyId Guide. ** If you don't use FriendlyId ** http://example.com/states/4323454 with FriendlyId http://example.com/states/washington There is a point that it is easy to identify by including the name in the URL, but by using uuid like our company, it becomes impossible to guess what number data it is, and it is also impossible to guess the size of the table. There is also.

You can also format URLs using a feature called slugged. In the example described in the FriendlyId Guide, if you put a character string with a space in the title as shown below, the URL should be like / posts / this% 20is% 20the% 20first% 20post. However, using Slugged makes it easier to read as / posts / this-is-the-first-post.

# a migration
class CreatePosts < ActiveRecord::Migration
  def self.up
    create_table :posts do |t|
      t.string :title, :null => false
      t.string :slug, :null => false
      t.text :body
    end

    add_index :posts, :slug, :unique => true
  end

  def self.down
    drop_table :posts
  end
end

# model
class Post < ActiveRecord::Base
  extend FriendlyId
  friendly_id :title, :use => :slugged
end
#Slugged makes it easier to see and shaped
@post = Post.create(:title => "This is the first post!)
@post.friendly_id # "this-is-the-first-post "Returns.
redirect_to @post #URL is/posts/this-is-the-first-It will be post.

important point

Let's judge whether or not FriendlyId is used in the model by looking at the description properly. It is a story that if you use it with the prejudice that you will use it in this model because you use FriendlyId in most models, it will be ???.

For example, in the model called company, there is a column called uuid, but it is not used as FriendlyId. company = Company.find("16d28548-2e57-4e67-a041-1f05acfdabba") If you write like company = Company.find(16) The result of is displayed. It seems that this is because the find method converts to an integer with .to_i when passing a string as an argument. If I get an error, I know I'm not using FriendlyId. Since the data was returned, it would be different for a moment if the data of ʻuuid = "16d28548-2e57-4e67-a041-1f05acfdabba" `was normally obtained.

Recommended Posts

Introduction and precautions of gem Friendly Id
Introduction and introduction of management screen generation gem Administrate
Introduction of Docker Hub and commands Self-learning ①
Introduction and usage explanation of Font Awesome
Introduction of pay.jp
Introduction of milkode
Introduction of Rspec and Japanese localization of error messages
Ruby and Gem
Automatic assignment of name attribute and id attribute by form_with
Introduction of Docker --Part 1--
[Rails] Introduction of PAY.JP
SystemSpec introduction and writing
Introduction of user authentication
Introduction purpose of ActiveHash and simple flow to application implementation