Make your own friendly id

While something like friendly_id sounds good, I think it’s a bit much for generating pretty urls.

This is from the friendly_id README:

Using FriendlyId, it’s easy to make your application use URL’s like:

…instead of:

A noble goal, to be sure. But once you starting digging into the gem, you realize that it provides a table of it’s own for tracking this stuff, plus it’s worried about what version of Active Record you’re using.

I don’t know about you, but I think it’s much easier to roll your own for this kind of thing. Less dependencies is generally a win in my experience.

Anyway, here’s a gist of a diff showing all of the steps necessary to generate a “friendly id” or “pretty url” in one go. It’s short enough that I think you’ll be able to sort it out for yourself:


Published by

Trevor Turk

A chess-playing machine of the late 18th century, promoted as an automaton but later proved a hoax.

3 thoughts on “Make your own friendly id”

  1. Something you're considering are the following:

    Accented characters and reserved words (new, edit, etc)

    Consistency over edits (old slugs no longer accessible results in 404 and by that broken old links).

    Also, after looking into friendly_id I can say that is one of the best plugin/gems code seen so far for this, not to mention concurrent support for both AR2 and AR3.

    If you think you need to put ActiveRecord on a diet, use friendly_id foundation called "babosa" which builds unicode-friendly slugs.

  2. I was also looking at friendly_id.

    The overhead (namely extra table) does seem quite significant. However it could prove useful for keeping a history of permalinks, perhaps for 301'ing old links in a CMS situation. In such instances with unpredictable end-users a more robust solution could be worth it. Different alphabets are handled well too.

    In most cases though, your lightweight parameterize solution is much more preferable. Less dependencies, as you say, is highly advantageous.

Comments are closed.