title: Rails Year Make Model Dynamic Menus
body: <p>Calling all Rails superheroes! I'm racking my brains with this one and can't find a solution anywhere online...I want to build year/make/model drop-downs to select a car from a table of 30,000+ different cars. The table is called <strong>car_variants</strong>. <a href="http://www.kbb.com/whats-my-car-worth/" rel="nofollow">Here's a link to a similar thing on kbb.org</a></p>
  
  <p>The <strong>Year</strong> is selected first which then populates the <strong>Make</strong> drop-down with those that were available in that year. This in turn populates the <strong>Model</strong> drop-down. All this filtering overcomes the dreadful problem of having the 30,000 car_variants in one unusable drop-down.</p>
  
  <p>Unfortunately I don't have enough reputation to post my entity diagram, but essentially...</p>
  
  <ul>
  <li>a car_make has many car_models </li>
  <li>a car_model has many car_variants</li>
  <li>a car_year has many car_variants</li>
  </ul>
  
  <p><strong>SIDE NOTE</strong> Since the car_variants table will be populated by importing an excel spreadsheet, I decided to join car_year to car_variants directly rather than saying a car_year has many car_models. That may or may not affect the challenge below.</p>
  
  <p>The <strong>car_variants</strong> model ( a table of virtually every car since 1984) stores the <strong>car_year_id</strong> and the <strong>car_model_id</strong>. (car_make is obvious through car_model)</p>
  
  <p>Now... along comes the user. They want to select their car from the car_variants table then add their car's <strong>nickname</strong> and <strong>license_plate</strong>. The user potentially has many cars so the car_variant_id's can't be stored in the user table. Instead we have a <strong>car</strong> table which contains the users cars.</p>
  
  <ul>
  <li>a car_variant has many cars (think of a car as being like an instance of a car_variant)</li>
  <li>a user has many cars</li>
  </ul>
  
  <p>Ryan Bates did an excellent <a href="http://railscasts.com/episodes/88-dynamic-select-menus-revised" rel="nofollow">screencast</a> about Dynamic Select Menus. In his example (Countries and States), he used grouped_collection_select and some javascript. That certainly appears to be heading in the right direction for this challenge too, but this is more complex because there are more has_many relationships than <strong>grouped_collection_select</strong> seems to be able to handle.</p>
  
  <p>I have successfully coded the car form with 3 drop-downs for year, make and model...</p>
  
  <pre><code>    &lt;%= simple_form_for(@car) do |f| %&gt;
        &lt;%= f.error_notification %&gt;
        &lt;div class="form-inputs"&gt; 
  
        &lt;!-- select year to filter make dropdown --&gt;
        &lt;p&gt;Year&lt;/p&gt;
          &lt;%= select_tag :car_year, options_for_select(CarYear.all.collect {|e| ["#{e.year}", e.id] }), :include_blank =&gt; true %&gt;
  
        &lt;!-- Select make to filter model dropdown --&gt;
        &lt;p&gt;Make&lt;/p&gt;
        &lt;%= select_tag :car_make, options_for_select(CarMake.all.collect {|e| ["#{e.name}", e.id] }), :include_blank =&gt; true %&gt;       
  
        &lt;!-- Select model --&gt;
        &lt;p&gt;Model (grouped collection select)&lt;/p&gt;
        &lt;%= f.grouped_collection_select :car_variant_id, CarMake.order(:name), :car_models, :name, :id, :name, include_blank: true %&gt;        
        &lt;%= f.input :nickname, :hint =&gt; "If you've given your car a nickname enter it here, otherwise just leave it blank." %&gt;
          &lt;%= f.input :plate %&gt;
          &lt;%= f.hidden_field :user_id, :value =&gt; current_user.id %&gt;
        &lt;/div&gt;
  
         &lt;div class="form-actions"&gt;
          &lt;%= f.button :submit, "Add Car" %&gt;
        &lt;/div&gt;
      &lt;% end %&gt;
  </code></pre>
  
  <p>...now how do I tie them all together so they filter one another then return the car_variant_id to be stored in the car_form.</p>
  
  <p>After several late bleary nights, I'm out of ideas.</p>