title: Connecting two models belongs_to/has_many and selecting via dropdown in form
body: <p>I am rails beginner and I am facing a basic problem. I have found a couple of solutions but none of them fully describe my problem.
  I have two models: Contact and Group</p>
  
  <pre><code>Group has_many :contacts
  Contact belongs_to :group
  </code></pre>
  
  <p>(Through devise there is a third user model)</p>
  
  <p>When creating a new contact I wish that the user will be able to select the group that this contact belongs to via a select option (like a dropdown field).</p>
  
  <p>I have added a :group_id index to he contacts table:</p>
  
  <pre><code>class AddGroupIdToContacts &lt; ActiveRecord::Migration
    def change
      add_column :contacts, :group_id, :integer
    end
  end
  </code></pre>
  
  <p>In the view/contacts/_form I have used the collection_select option to output a dropdown field showing the available groups:</p>
  
  <pre><code>&lt;%= form_for(@contact) do |f| %&gt;
    &lt;% if @contact.errors.any? %&gt;
      &lt;div id="error_explanation"&gt;
        &lt;h2&gt;&lt;%= pluralize(@contact.errors.count, "error") %&gt; prohibited this contact from being saved:&lt;/h2&gt;
  
        &lt;ul&gt;
        &lt;% @contact.errors.full_messages.each do |message| %&gt;
          &lt;li&gt;&lt;%= message %&gt;&lt;/li&gt;
        &lt;% end %&gt;
        &lt;/ul&gt;
      &lt;/div&gt;
    &lt;% end %&gt;
  
    ...
  
    &lt;div class="field"&gt;
      &lt;%= f.label :group_id %&gt;&lt;br&gt;
      &lt;%= f.collection_select(:group_id, Group.all, :id, :name) %&gt;
    &lt;/div&gt;
  
    ...
  
    &lt;div class="actions"&gt;
      &lt;%= f.submit %&gt;
    &lt;/div&gt;
  &lt;% end %&gt;
  </code></pre>
  
  <p>I can input a new contact successfully but when checking in the console I have an empty group_id: nil field.</p>
  
  <p>What is the correct way to input the id of the selected Group into the group_id field and how can I display this entry (preferably by using the name of the groups id) in the show.html.erb? </p>
  
  <p>I know this is a basic questions but I stuck and pretty new to rails. Thanks for your help!</p>