title: Rails - Dynamic Select - Collection Select
body: <p>I've been trying to get some dynamic select functionality working, but despite many different tutorial i've yet to get it to work. For ease of reading, i've brought the code examples down to basics. Any advise would be greatly appreciated.</p>
  
  <p>On the faults page, i need to assign a company and contact to the fault, but I only want to be able to see the contacts associated with the selected company</p>
  
  <pre><code>Fault - belongs_to :company, :user, :contact
  User - has_many :faults
  Contacts - has_and_belongs_to_many :companies
  Company - has_and_belongs_to_many :contacts, has_many :faults
  </code></pre>
  
  <p>/faults/_form.html.erb</p>
  
  <pre><code>&lt;%= f.label :company, "Company:" %&gt;
  
  &lt;%= collection_select(:fault,:company_id,@companies,:id,:full_name, :prompt =&gt; "Please select a company") %&gt;&lt;/br&gt;
  
  &lt;%= f.label :contact, "Contact:" %&gt;
  
  &lt;%= f.collection_select :contact_id, @contacts, :id, :name, :prompt =&gt; "Select a Contact" %&gt;
  
  &lt;%= link_to "Add New Contact", {:controller =&gt; "companies", :action =&gt; "index"}, :confirm =&gt; "To add a contact for a company you need to do this from the companies page." %&gt;&lt;/br&gt;
  </code></pre>