title: nested form rails 4 save existing record on create
body: <p>I am struggling to get this working. I have three models </p>
  
  <ol>
  <li>Student</li>
  <li>Classroomattnd</li>
  <li>Classroom</li>
  </ol>
  
  <p>Using the has_many :through relationship. All my relationships are defined correctly and I have setup the nested form using the accepts_nested_attributes.</p>
  
  <p>So when creating a new student I want to select from a list of classrooms instead of creating a new classroom. The form part also works fine the part I am not getting is when I create the student it complains about the following error.</p>
  
  <p>Couldn't find Classrooom with ID=3 for Student with ID=</p>
  
  <p>I have searched around for few days now but can not get the answer I need to get this working.</p>
  
  <pre><code>def new
    @student = Student.new
    @student.classrooms.build
  end
  
  def edit
  end
  
  def create
    @student = Student.new(student_params)
  
    respond_to do |format|
      if @student.save
        format.html { redirect_to @student, notice: 'Student was successfully created.' }
        format.json { render :show, status: :created, location: @student }
      else
        format.html { render :new }
        format.json { render json: @student.errors, status: :unprocessable_entity }
      end
    end
  end
  </code></pre>
  
  <p>Can someone help here, someone must of face this issue before?</p>
  
  <p>Also in the rails console when I run the following it works:</p>
  
  <pre><code>classroom = Classroom.last
  student = Student.create(name: 'Dave', classrooms:[classroom])
  </code></pre>