title: Model and controller designs in rails for friendly URL
body: <p>Another question from rails newbie. I am using friendly_id gem with mysql in rails 3.x </p>
  
  <p>This is a design problem (may be easy in rails). I am expecting advises from rails experts. I am building a library listing app. Where user can view library by "metro-area" or by "city" in it. For example:</p>
  
  <p>I wish to have URLs like:</p>
  
  <p>www.list.com/library/san-francisco-bay-area</p>
  
  <p>or</p>
  
  <p>www.list.com/library/san-francisco-bay-area/palo-alto/</p>
  
  <p>In database I have tables:</p>
  
  <pre><code>library
  -------
  id, name, city_id, slug
  </code></pre>
  
  <p>name is slugged here and city_id is FK</p>
  
  <pre><code>city
  ----
  city_id, name, metro_area_id, slug
  </code></pre>
  
  <p>name is slugged here and metro_area_id is FK</p>
  
  <pre><code>metro_area 
  ----------
  metro_area_id, name, state, slug
  </code></pre>
  
  <p>name is slugged here</p>
  
  <p>So when a user points browser to www.list.com/library/san-francisco-bay-area/palo-alto</p>
  
  <p>I wish to get list of libraries in san-francisco-bay-area/palo-alto. But my library table model is containing slug only for library's name. So how this URL can be parsed to find the city_id that can be used in library model and controller to get the list. </p>
  
  <p>Please remember, I cannot rely only on the name of the city. I would have to find city_id of 'palo-alto' which <em>is in</em> metro 'san-francisco-bay-area'. Since slug for metro_area and city is in other tables, so what is the best way to design model and controller. </p>
  
  <p>Show method of controller is:</p>
  
  <pre><code>def show
      @user = Library.find(params[:id])
  
      respond_to do |format|
         format.html # show.html.erb
         format.json { render json: @library }
      end
  end
  </code></pre>
  
  <p>and model is</p>
  
  <pre><code>class Library &lt; ActiveRecord::Base
    attr_accessible :name, :city_id, :slug
    extend FriendlyId
    friendly_id :name, use: :slugged
  end
  </code></pre>
  
  <p>This will not work per my friendly URL requirement. So I would appreciate advice from experts :)</p>