Using static name as unique key instead of id in some table

Hi,

I am not sure if this is best practice or not. or it is a stupid
question.

Using static name as unique key instead of id in some table.
What I mean by static name is the unique name from the actual field name
For example : “San francisco” become “san_francisco” (it automatically
replace space or others not character to “_”)

Reason to use this way

  1. User friendly url
    so someone can type domain.com/san_francisco

Cons of not using it

  1. need to customize route.rb to use static name
  2. others table that reference this
  3. performance in database indexing
  4. slower database query
  5. Others table start to use “static_name” instead of id to reference to
    this table

Thank you in advance, any feedback are welcome

I believe you can have the besto of both worlds:

In the City model (as u said san francisco):

validate_uniqueness_of :name

in the routes

map.connect ‘/:city_name’, :controller => ‘cities’, :action => ‘show’

in the controller

def show
@city = City.find_by_name(params[:city_name])
end

you can have id as all other tables (not having it just seems too bad
idea)
you can have pretty urls

j

On Jun 26, 3:00 pm, Beta B. [email protected]

There’s an even easier way. Use the friendly identifier plugin:

http://agilewebdevelopment.com/plugins/friendly_identifier

With a single line of code in your model:

friendly_identifier :name, :identifier_column => :name

you get the pretty URLs you want without breaking the Rails default
behavior with id’s.

Beta B. wrote:

Hi,

I am not sure if this is best practice or not. or it is a stupid
question.

Using static name as unique key instead of id in some table.
What I mean by static name is the unique name from the actual field name
For example : “San francisco” become “san_francisco” (it automatically
replace space or others not character to “_”)

Reason to use this way

  1. User friendly url
    so someone can type domain.com/san_francisco

Cons of not using it

  1. need to customize route.rb to use static name
  2. others table that reference this
  3. performance in database indexing
  4. slower database query
  5. Others table start to use “static_name” instead of id to reference to
    this table

Thank you in advance, any feedback are welcome

Excellent tips, thanks Wolas and Brent M.