To start off, the friendly_id gem allows your models to have “slugs” which ultimately help when you want your urls to appear more descriptive. For example instead of site.com/cars/43  you will get something like: site.com/cars/slug-name .

It’s better for SEO and IMO provides a better experience for situations where someone copies a link and sends it to another individual.  When you see a descriptive URL you kind of know what to expect and it feels less threatening than site.com/?blah=92349&whatever=5959 ….. OR i’m just a wussy.

My current situation is this:
I have some STI models that use inheritance and want to use friendly_id with it.

The problem i ran into and erroneously thought was a bug can be found here:
https://github.com/norman/friendly_id/issues/167 .  Basically when creating records for inheriting models i was getting duplicate slug errors.  Something like: “Error: Duplicate entry blah blah”.

A quick answer the gem’s author, Norman, rained light and joy on where i had gone wrong.

The Solution:

So how do you get STI models and friendly_id to work together?  This gist says it all and uses the idea of cars/brands as the STI model.  Note: this is really only for example, if you have a lot of cars and brands you will probably want to model that differently.

Read the docs:

The gem’s docs are really good.  While the docs for the FriendlyId::Scoped module explain everything in concise terms here they do not explicitly cover STI so hopefully this helps you out if you’re in the same situation.

My use case for this is that i have a rails app using STI.  I’ve got an array of results from all of my inheriting models and i’d like to sort them ALL based on a common attribute they share — a zip code.

Here is a RIDICULOUSLY simple way to use Ruby to sort the entire array — please note though, the code is querying for “Model.all” just to make the example simple — in my REAL code i have more complicated search criteria.

 

I’m using Twitter’s Bootstrap CSS framework and it’s pretty nice.  One of the cool bundled features of the framework is that they provide classes for displaying what rails hackers know as Flash messages.  The problem here is that Twitter has named them different from what Rails people traditionally use so you cannot just drop in Bootstrap and expect your flash messages to appear properly.

I did NOT want to modify the original bootstrap css and tack on extra classes so I got around this with an application helper method that does some translating for me.

Here is the code: