Beta Beta
2008-Jun-26 14:00 UTC
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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
\"Wolas!\"
2008-Jun-26 15:55 UTC
Re: Using static name as unique key instead of id in some table
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 Beta <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> 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 > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Brent Miller
2008-Jun-26 16:10 UTC
Re: Using static name as unique key instead of id in some ta
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 Beta 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-- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Beta Beta
2008-Jun-26 16:50 UTC
Re: Using static name as unique key instead of id in some ta
Excellent tips, thanks Wolas and Brent Miller -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---