title: Rails Dynamically Changing Collection Select In Form Based on form field
body: <p>I have a Rails 3.2.14 app that tracks calls and each call has a pickup and dropoff facility from the Facility model which is an association.</p>
  
  <p>In the call model there is an association for the Region model (i.e. Houston, Dallas, Austin, etc) where we select a region based on where the call is coming from.</p>
  
  <p>What I'd like to do is be able to select a specific region (i.e. Houston) and in the pickup facility collection only show Houston region facilities.  </p>
  
  <p>I'm assuming to start off with I'd need to setup a relationship between Facility and Region as such:</p>
  
  <p><strong>facility.rb</strong></p>
  
  <pre><code>attr_accessible :region_id
  belongs_to :region
  </code></pre>
  
  <p><strong>region.rb</strong></p>
  
  <pre><code>has_many :facilities
  </code></pre>
  
  <p>Then I'd need to set each facility with a region_id that matches the respective region (i.e Houston, Dallas, etc) so the association works properly.</p>
  
  <p>What I'm unsure of is how to select the specific region and only have the facilities in that region display in the form.  I'm assuming I would use some jQuery/JS/Ajax here to make it happen but not sure how to make it work.</p>
  
  <p>Here's an excerpt of what my call, facility, and region models currently look like:</p>
  
  <p><strong>call.rb</strong></p>
  
  <pre><code>belongs_to :transferred_from, :foreign_key =&gt; :transfer_from_id, :class_name =&gt; 'Facility'
  belongs_to :transferred_to, :foreign_key =&gt; :transfer_to_id, :class_name =&gt; 'Facility'
  belongs_to :region
  </code></pre>
  
  <p><strong>facility.rb</strong></p>
  
  <pre><code>has_many :calls_transferred_from, :foreign_key =&gt; :transfer_from_id, :class_name =&gt; 'Call'
  has_many :calls_transferred_to, :foreign_key =&gt; :transfer_to_id, :class_name =&gt; 'Call'
  </code></pre>
  
  <p><strong>region.rb</strong></p>
  
  <pre><code>has_many :calls
  </code></pre>
  
  <p>Here's an excerpt of what my call form partial looks like:</p>
  
  <p><strong>_form.html.erb</strong></p>
  
  <pre><code>&lt;%= form_for(@call) do |f| %&gt;
    &lt;%= f.label :region %&gt;
    &lt;%= f.collection_select(:region_id, Region.all, :id, :area, {:include_blank =&gt; true}, {:class =&gt; 'select', required: true}) %&gt;
    &lt;%= f.label :Transfer_From %&gt;
    &lt;%= f.collection_select(:transfer_from_id, Facility.active.order("facility_name ASC"), :id, :facility_name, {:include_blank =&gt; true}, {:class =&gt; 'select'}) %&gt;
   &lt;%= f.button "Submit", class: 'btn btn-info btn-large', data: {disable_with: "&lt;i class='icon-spinner'&gt;&lt;/i&gt;Processing..."} %&gt; 
  &lt;% end %&gt;
  </code></pre>
  
  <p>If any of this is confusing or needs clarification, please let me know.  To summarize, I'm trying to select a region (Houston) and only show the Facilities that are in the Houston region in the form when that region is selected.</p>
  
  <p>Thanks in advance for any help or tips you can provide.</p>