title: Create multiple relationships
body: <p>I am developing a Q&amp;A app and the thing is, I want to add the ability for people to post answers.</p>
  
  <p>I already built a migration so that the question has the same id of the user with
  <code>rails g migration add_user_id_to_questions</code> and then added the <code>has_many/belongs_to</code> associations.</p>
  
  <p>Now I am stuck with how to create a relationship for the answers.</p>
  
  <p>I already added a answer scaffold with:
  <code>rails g scaffold Answer answer:integer</code></p>
  
  <p>And then I did a migration: (Was it a right move? to create a migration so that answers have the same ID as the question?)</p>
  
  <p><code>rails g migration add_answer_id_to_answers</code></p>
  
  <p>And added the <code>has_many/belong_to</code> as well.</p>
  
  <p>But now what? How do I make answer the to have the same ID of the question? Because inside the show view, if a user posts an answer it will basically be visible on every question...</p>
  
  <p>Show view:</p>
  
  <pre><code>      &lt;% @answers.each do |answer| %&gt;
  
        &lt;div class="well bgBlack"&gt;
            &lt;div class="media"&gt;
              &lt;a class="pull-left"&gt;
              &lt;% if answer.user.avatar.blank? %&gt;
                  &lt;img src="http://www.adtechnology.co.uk/images/UGM-default-user.png" style="width: 75px;"&gt;
              &lt;% elsif answer.user.avatar %&gt;
                  &lt;%= image_tag answer.user.avatar, :style =&gt; "width:75px;" %&gt;
              &lt;% end %&gt;
              &lt;/a&gt;
              &lt;div class="media-body"&gt;
                &lt;p class="white"&gt;&lt;%= link_to answer.user.username, answer.user, :class =&gt; " bg" %&gt;&lt;/p&gt;
                &lt;p class="white small"&gt;&lt;%= answer.answer %&gt;&lt;/p&gt;
             &lt;/div&gt;
             &lt;div class="pull-right"&gt;
                  &lt;% if user_signed_in? %&gt;
                      &lt;% if answer.user_id == current_user.id %&gt;
                          &lt;%= link_to 'delete', answer_path(answer), :method =&gt; :delete, :class =&gt; "text-muted white" %&gt;
                      &lt;% end %&gt;
                  &lt;% end %&gt;
             &lt;/div&gt;
          &lt;/div&gt;
        &lt;/div&gt;                
  
        &lt;% end %&gt;
  </code></pre>
  
  <p>User model:</p>
  
  <pre><code>class User &lt; ActiveRecord::Base
    mount_uploader :avatar, AvatarUploader
    has_many :questions, :dependent =&gt; :destroy
    has_many :answers, :dependent =&gt; :destroy
  
    devise :database_authenticatable, :registerable,
           :recoverable, :rememberable, :trackable, :validatable
  end
  </code></pre>
  
  <p>Question model:</p>
  
  <pre><code>class Question &lt; ActiveRecord::Base
      belongs_to :user
      has_many :answers
  end
  </code></pre>
  
  <p>Answer model:</p>
  
  <pre><code>class Answer &lt; ActiveRecord::Base
      belongs_to :user
      belongs_to :question
  end
  </code></pre>
  
  <p>Answer controller:</p>
  
  <pre><code>class AnswersController &lt; ApplicationController
    before_action :set_answer, only: [:destroy]
  
    respond_to :html
  
    def create
      @answer = current_user.answers.build(answer_params)
      @answer.save
      redirect_to :back
    end
  
    def destroy
      @answer.destroy
      redirect_to :back
    end
  
    private
      def set_answer
        @answer = Answer.find(params[:id])
      end
  
      def answer_params
        params.require(:answer).permit(:answer)
      end
  end
  </code></pre>