views:

74

answers:

3

Say I have http://www.mysite.com/I-Like-Cheeseburgers and I want that to point to Item with id 3. Sometime later, I change the name of the item, and now its http://www.mysite.com/I-Like-Hamburgers (and perhaps many more times). I want all these URLs to remain pointing to Item 3. Is it efficient to simply keep a table of [strings,item_ids] and do a lookup on this? Is there a better way?

+4  A: 

Or you can do like stackoverflow.com

http://stackoverflow.com/questions/2887068/seo-friendly-urls-where-the-phrase-used-may-change-in-rails

You keep the question ID (2887068) in the link, where the text part can be changed and still will land in the same page. http://stackoverflow.com/questions/2887068/whatever-you-put or http://stackoverflow.com/questions/2887068/ will send you to the same page.

so for your example it would be :

http://www.mysite.com/items/3/I-Like-Cheeseburgers
Bakkal
+2  A: 

You can also use plugin friendly_id.

With :use_slug option it maintains history of permalinks and offer solutions to redirect to current URL etc.

class YourModel < ActiveRecord::Base
  has_friendly_id :title, :use_slug => true
end

See documentation for more.

Voyta
A: 

in your model do

class Article < AR::Base
  def to_param
    "#{id}-#{name.parameterize}"
  end
end

Your links will look like: http://mysite.com/articles/3-i-like-cheeseburgers

When you try to fetch the object, doing Article.find(params[:id]) would automagically convert the "3-i-like-cheeseburgers" into an integer and fetch the correct object. This allows the name to change, while maintaining the link to point to the same article object.

Faisal