has_permalink — SEO Permalink Plugin for Ruby on Rails

Many Rails developers take the easy way out when it comes to handling their permalinks. They use the models database id followed by a hyphen and some string, like this:


I've seen this pattern on many sites and presented in many tutorials as the right way to stuff keywords in the url. This technique often takes advantage of Rubys to_i method which converts a string to an integer and removes any non-numeric characters.

irb: '956546-some-string-with-keywords'.to_i
irb: => 956546

This technique requires only minor changes from the default scaffold in your controller.


It's quick and easy but exposes some serious problems:

  • It's possible to link to you using any string and it could get indexed that way. That'll screw up your statistics, drain your link juice and lower your PageRank.
  • It's impossible to remember and hard to find again since the integer is positioned first.
  • It lowers your keyword density and the important phrase comes last.

The url must be unique, permanent, relevant, user friendly and easy to remember. So don't use this technique again... Ever!

Your Cure for Link Juice Draining, Keyword Density Lowering & Confusing Urls

Meet has_permalink. A ruby gem for generating the perfect structure of an url from any other attribute in the same ActiveRecord model. The easy and lazy, but robust way.

It takes any other method from the same ActiveRecord model and converts it to a url-safe string.

has_permalink is well tested has a strong user base and contains lots helpful commands. It gives you full control over your permalink structure via your routes file.


  • Easy to use and includes some helpful commands
  • Provides full control over the permalinks structure
  • No external dependencies
  • Url's doesn't change even if the title does
  • Works with all european characters like å, ä and ö

1 Minute Easy Installation

Put this in your gemfile:

gem 'has_permalink'

And run the following command in your terminal:

$ bundle install

To generate the permalink attribute, use this generator:

$ rails g has_permalink [Model]

This generates a permalinks migration which adds the permalink attribute to [Model] and also an index for faster sql queries.

Don't forget to:

$ rake db:migrate


Just add this line to your model and the permalink attribute is generated within the before_validation callback:

class Post < ActiveRecord::Base

Assuming the models title is set to 'Some string' the generated permalink will be set to 'some-string' before the model is validated.

Are you're developing an application for another language than english?

No problem! For instance the swedish phrase 'Bättre brödlös än rådlös' is converted to 'battre-brodlos-an-radlos'. This is something most other Rails permalink plugins fail to accomplish. Instead it leaves you with a disappointing bttre-brdls-n-rdls. This is infact one of the main reasons I created the gem.

By default has_permalink will use the title attribute to generate the permalink but you can override it by passing an argument like this:

class Post < ActiveRecord::Base
  has_permalink :heading

Feel free to pass any method as an argument. For example:

class User < ActiveRecord::Base
  has_permalink :full_name

  def full_name
    "#{first_name} #{last_name}"

Since all urls should be permanent, the permalink is only transformed if it's blank. You can however force regeneration by using this method:


Change your show action to query your model by the permalink attribute:

def show
  @post = Post.find_by_permalink(params[:id])

If you followed these steps to add has_permalink to an existing application where you already had lots of data then all permalink attributes are blank. If that's the case; use this convenient rake task:

$ rake has_permalink:generate_permalinks[Model]

That's it. You're done. Your urls are now unique, permanent, relevant, user friendly and easy to remember.


  • Extend ActiveModel instead of ActiveRecord
  • Versioned permalinks with 301 redirects
  • Make sure it works with Sinatra, maybe it does. I don't know.

Your Contribution

If you find anything and I mean anything you would like to change or improve. Maybe you have some ideas about a new feature that would improve the gem or perhaps you found a bug or a typo in this article. Feel free to contact me with a comment below.

So, do as many other Rails developers. Download has_permalink, try it out and post a comment with your feedback.


Ola Karlsson

has_permalink is created and maintained by Ola Karlsson, Software Craftsman at Byteamin.

Fork me on GitHub