Skip to content
Fetching contributors…
Cannot retrieve contributors at this time
466 lines (325 sloc) 14 KB

#encoding: utf-8

About FriendlyId

FriendlyId is an add-on to Ruby's Active Record that allows you to replace ids in your URLs with strings:

# without FriendlyId

# with FriendlyId

It requires few changes to your application code and offers flexibility, performance and a well-documented codebase.

Core Concepts


The concept of “slugs” is at the heart of FriendlyId.

A slug is the part of a URL which identifies a page using human-readable keywords, rather than an opaque identifier such as a numeric id. This can make your application more friendly both for users and search engine.

Finders: Slugs Act Like Numeric IDs

To the extent possible, FriendlyId lets you treat text-based identifiers like normal IDs. This means that you can perform finds with slugs just like you do with numeric ids:


Setting Up FriendlyId in Your Model

To use FriendlyId in your ActiveRecord models, you must first extend the FriendlyId module, then invoke the {FriendlyId::Base#friendly_id friendly_id} method to configure your desired options:

class Foo < ActiveRecord::Base
  extend FriendlyId
  friendly_id bar, :use => [:slugged, :i18n]

The most important option is `:use`, which you use to tell FriendlyId which addons it should use. See the documentation for this method for a list of all available addons, or skim through the rest of the docs to get a high-level overview.

The Default Setup: Simple Models

The simplest way to use FriendlyId is with a model that has a uniquely indexed column with no spaces or special characters, and that is seldom or never updated. The most common example of this is a user name:

class User < ActiveRecord::Base
  extend FriendlyId
  friendly_id :login
  validates_format_of :login, :with => /\A[a-z0-9]+\z/i

@user = User.find "joe"   # the old User.find(1) still works, too
@user.to_param            # returns "joe"
redirect_to @user         # the URL will be /users/joe

In this case, FriendlyId assumes you want to use the column as-is; it will never modify the value of the column, and your application should ensure that the value is unique and admissible in a URL:

class City < ActiveRecord::Base
  extend FriendlyId
  friendly_id :name

@city.find "Viña del Mar"
redirect_to @city # the URL will be /cities/Viña%20del%20Mar

Writing the code to process an arbitrary string into a good identifier for use in a URL can be repetitive and surprisingly tricky, so for this reason it's often better and easier to use {FriendlyId::Slugged slugs}.

Slugged Models

FriendlyId can use a separate column to store slugs for models which require some text processing.

For example, blog applications typically use a post title to provide the basis of a search engine friendly URL. Such identifiers typically lack uppercase characters, use ASCII to approximate UTF-8 character, and strip out other characters which may make them aesthetically unappealing or error-prone when used in a URL.

class Post < ActiveRecord::Base
  extend FriendlyId
  friendly_id :title, :use => :slugged

@post = Post.create(:title => "This is the first post!")
@post.friendly_id   # returns "this-is-the-first-post"
redirect_to @post   # the URL will be /posts/this-is-the-first-post

In general, use slugs by default unless you know for sure you don't need them. To activate the slugging functionality, use the {FriendlyId::Slugged} module.

FriendlyId will generate slugs from a method or column that you specify, and store them in a field in your model. By default, this field must be named :slug, though you may change this using the {FriendlyId::Slugged::Configuration#slug_column slug_column} configuration option. You should add an index to this column, and in most cases, make it unique. You may also wish to constrain it to NOT NULL, but this depends on your app's behavior and requirements.

Example Setup

# your model
class Post < ActiveRecord::Base
  extend FriendlyId
  friendly_id :title, :use => :slugged
  validates_presence_of :title, :slug, :body

# 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

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

  def self.down
    drop_table :posts

Working With Slugs


By default, FriendlyId uses Active Support's paramaterize method to create slugs. This method will intelligently replace spaces with dashes, and Unicode Latin characters with ASCII approximations:

movie = Movie.create! :title => "Der Preis fürs Überleben"
movie.slug #=> "der-preis-furs-uberleben"


When you try to insert a record that would generate a duplicate friendly id, FriendlyId will append a sequence to the generated slug to ensure uniqueness:

car = Car.create :title => "Peugot 206"
car2 = Car.create :title => "Peugot 206"

car.friendly_id #=> "peugot-206"
car2.friendly_id #=> "peugot-206--2"

Sequence Separator - The Two Dashes

By default, FriendlyId uses two dashes to separate the slug from a sequence.

You can change this with the {FriendlyId::Slugged::Configuration#sequence_separator sequence_separator} configuration option.

Column or Method?

FriendlyId always uses a method as the basis of the slug text - not a column. It first glance, this may sound confusing, but remember that Active Record provides methods for each column in a model's associated table, and that's what FriendlyId uses.

Here's an example of a class that uses a custom method to generate the slug:

class Person < ActiveRecord::Base
  friendly_id :name_and_location
  def name_and_location
    "#{name} from #{location}"

bob = Person.create! :name => "Bob Smith", :location => "New York City"
bob.friendly_id #=> "bob-smith-from-new-york-city"

Providing Your Own Slug Processing Method

You can override {FriendlyId::Slugged#normalize_friendly_id} in your model for total control over the slug format.

Deciding When to Generate New Slugs

Overriding {FriendlyId::Slugged#should_generate_new_friendly_id?} lets you control whether new friendly ids are created when a model is updated. For example, if you only want to generate slugs once and then treat them as read-only:

class Post < ActiveRecord::Base
  extend FriendlyId
  friendly_id :title, :use => :slugged

  def should_generate_new_friendly_id?

post = Post.create!(:title => "Hello world!")
post.slug #=> "hello-world"
post.title = "Hello there, world!"!
post.slug #=> "hello-world"

Locale-specific Transliterations

Active Support's parameterize uses transliterate, which in turn can use I18n's transliteration rules to consider the current locale when replacing Latin characters:

# config/locales/de.yml
        ü: "ue"
        ö: "oe"

movie = Movie.create! :title => "Der Preis fürs Überleben"
movie.slug #=> "der-preis-fuers-ueberleben"

This functionality was in fact taken from earlier versions of FriendlyId.

Gotchas: Common Problems

FriendlyId uses a before_validation callback to generate and set the slug. This means that if you create two model instances before saving them, it's possible they will generate the same slug, and the second save will fail.

This can happen in two fairly normal cases: the first, when a model using nested attributes creates more than one record for a model that uses friendly_id. The second, in concurrent code, either in threads or multiple processes.

Nested Attributes

To solve the nested attributes issue, I recommend simply avoiding them when creating more than one nested record for a model that uses FriendlyId. See this Github issue for discussion.


To solve the concurrency issue, I recommend locking the model's table against inserts while when saving the record. See this Github issue for discussion.

History: Avoiding 404's When Slugs Change

FriendlyId's {FriendlyId::History History} module adds the ability to store a log of a model's slugs, so that when its friendly id changes, it's still possible to perform finds by the old id.

The primary use case for this is avoiding broken URLs.


In order to use this module, you must add a table to your database schema to store the slug records. FriendlyId provides a generator for this purpose:

rails generate friendly_id
rake db:migrate

This will add a table named friendly_id_slugs, used by the {FriendlyId::Slug} model.


This module is incompatible with the :scoped module.

Because recording slug history requires creating additional database records, this module has an impact on the performance of the associated model's create method.


class Post < ActiveRecord::Base
  extend FriendlyId
  friendly_id :title, :use => :history

class PostsController < ApplicationController

  before_filter :find_post


  def find_post
    Post.find params[:id]

    # If an old id or a numeric id was used to find the record, then
    # the request path will not match the post_path, and we should do
    # a 301 redirect that uses the current friendly id.
    if request.path != post_path(@post)
      return redirect_to @post, :status => :moved_permanently

Unique Slugs by Scope

The {FriendlyId::Scoped} module allows FriendlyId to generate unique slugs within a scope.

This allows, for example, two restaurants in different cities to have the slug joes-diner:

class Restaurant < ActiveRecord::Base
  extend FriendlyId
  belongs_to :city
  friendly_id :name, :use => :scoped, :scope => :city

class City < ActiveRecord::Base
  extend FriendlyId
  has_many :restaurants
  friendly_id :name, :use => :slugged


Without :scoped in this case, one of the restaurants would have the slug joes-diner and the other would have joes-diner--2.

The value for the :scope option can be the name of a belongs_to relation, or a column.

Finding Records by Friendly ID

If you are using scopes your friendly ids may not be unique, so a simple find like


may return the wrong record. In these cases it's best to query through the relation:


Alternatively, you could pass the scope value as a query parameter:

Restaurant.find("joes-diner").where(:city_id =>

Finding All Records That Match a Scoped ID

Query the slug column directly:


Routes for Scoped Models

Recall that FriendlyId is a database-centric library, and does not set up any routes for scoped models. You must do this yourself in your application. Here's an example of one way to set this up:

# in routes.rb
resources :cities do
  resources :restaurants

# in views
<%= link_to 'Show', [@city, @restaurant] %>

# in controllers
@city = City.find(params[:city_id])
@restaurant = @city.restaurants.find(params[:id])

# URLs:

Basic I18n

This {FriendlyId::I18n i18n} adds very basic i18n support to FriendlyId.

In order to use this module, your model must have a slug column for each locale. By default FriendlyId looks for columns named, for example, “slug_en”, “slug_es”, etc. The first part of the name can be configured by passing the :slug_column option if you choose. Note that as of 4.0.0.beta11, the column for the default locale must also include the locale in its name.

Example migration

def self.up
  create_table :posts do |t|
    t.string :title
    t.string :slug_en
    t.string :slug_es
    t.text   :body
  add_index :posts, :slug_en
  add_index :posts, :slug_es


Finds will take into consideration the current locale:

I18n.locale = :es
I18n.locale = :en

To find a slug by an explicit locale, perform the find inside a block passed to I18n's with_locale method:

I18n.with_locale(:es) do

Creating Records

When new records are created, the slug is generated for the current locale only.

Translating Slugs

To translate an existing record's friendly_id, use {FriendlyId::I18n::Model#set_friendly_id}. This will ensure that the slug you add is properly escaped, transliterated and sequenced:

post = Post.create :name => "Star Wars"
post.set_friendly_id("La guerra de las galaxas", :es)

If you don't pass in a locale argument, FriendlyId::I18n will just use the current locale:

I18n.with_locale(:es) do

Reserved Words

The {FriendlyId::Reserved Reserved} module adds the ability to exlude a list of words from use as FriendlyId slugs.

By default, FriendlyId reserves the words “new” and “edit” when this module is included. You can configure this globally by using {FriendlyId.defaults FriendlyId.defaults}:

FriendlyId.defaults do |config|
  config.use :reserved
  # Reserve words for English and Spanish URLs
  config.reserved_words = %w(new edit nueva nuevo editar)
Something went wrong with that request. Please try again.