title: How Would You Generate Hash with Nested Attributes for Rspec Controller #create Test
body: <p>I am testing Rails 4 controller <code>#create</code> action in Rspec as following:</p>
  
  <pre><code>describe "POST #create" do    
      it "saves personality test responses in database" do
        expect {
          post :create, graduate_id: graduate, personality_test: attributes_for(...)
        }.to change(PersonalityTest, :count).by(1)
      end
  end
  </code></pre>
  
  <p>Note, the <code>attributes_for(...)</code> above is the part I am struggling with. In order for the controller to create a new record, I need to pass a hash in this format (where <code>"personality_test_template_question_id"</code> and <code>"personality_test_template_answer_id"</code> are foreign keys to existing models:</p>
  
  <pre><code>"personality_test"=&gt;{
    "personality_test_template_id"=&gt;"1", "personality_test_responses_attributes"=&gt;{
      "0"=&gt;{"personality_test_template_question_id"=&gt;"1", "personality_test_template_answer_id"=&gt;"2"}, 
      "1"=&gt;{"personality_test_template_question_id"=&gt;"2", "personality_test_template_answer_id"=&gt;"9"}, 
      "2"=&gt;{"personality_test_template_question_id"=&gt;"3", "personality_test_template_answer_id"=&gt;"14"}, 
      "3"=&gt;{"personality_test_template_question_id"=&gt;"4", "personality_test_template_answer_id"=&gt;"21"}, 
      "4"=&gt;{"personality_test_template_question_id"=&gt;"5", "personality_test_template_answer_id"=&gt;"27"}, 
      "5"=&gt;{"personality_test_template_question_id"=&gt;"6", "personality_test_template_answer_id"=&gt;"33"}, 
      "6"=&gt;{"personality_test_template_question_id"=&gt;"7", "personality_test_template_answer_id"=&gt;"38"}, 
      "7"=&gt;{"personality_test_template_question_id"=&gt;"8", "personality_test_template_answer_id"=&gt;"46"}, 
      "8"=&gt;{"personality_test_template_question_id"=&gt;"9", "personality_test_template_answer_id"=&gt;"54"}, 
      "9"=&gt;{"personality_test_template_question_id"=&gt;"10", "personality_test_template_answer_id"=&gt;"58"}, 
      "10"=&gt;{"personality_test_template_question_id"=&gt;"11", "personality_test_template_answer_id"=&gt;"64"}, 
      "11"=&gt;{"personality_test_template_question_id"=&gt;"12", "personality_test_template_answer_id"=&gt;"70"}, 
      "12"=&gt;{"personality_test_template_question_id"=&gt;"13", "personality_test_template_answer_id"=&gt;"76"}, 
      "13"=&gt;{"personality_test_template_question_id"=&gt;"14", "personality_test_template_answer_id"=&gt;"83"}, 
      "14"=&gt;{"personality_test_template_question_id"=&gt;"15", "personality_test_template_answer_id"=&gt;"86"}, 
      "15"=&gt;{"personality_test_template_question_id"=&gt;"16", "personality_test_template_answer_id"=&gt;"92"}, 
      "16"=&gt;{"personality_test_template_question_id"=&gt;"17", "personality_test_template_answer_id"=&gt;"101"}, 
      "17"=&gt;{"personality_test_template_question_id"=&gt;"18", "personality_test_template_answer_id"=&gt;"107"}, 
      "18"=&gt;{"personality_test_template_question_id"=&gt;"19", "personality_test_template_answer_id"=&gt;"110"}, 
      "19"=&gt;{"personality_test_template_question_id"=&gt;"20", "personality_test_template_answer_id"=&gt;"119"}, 
      "20"=&gt;{"personality_test_template_question_id"=&gt;"21", "personality_test_template_answer_id"=&gt;"122"}, 
      "21"=&gt;{"personality_test_template_question_id"=&gt;"22", "personality_test_template_answer_id"=&gt;"132"}, 
      "22"=&gt;{"personality_test_template_question_id"=&gt;"23", "personality_test_template_answer_id"=&gt;"137"}, 
      "23"=&gt;{"personality_test_template_question_id"=&gt;"24", "personality_test_template_answer_id"=&gt;"140"}, 
      "24"=&gt;{"personality_test_template_question_id"=&gt;"25", "personality_test_template_answer_id"=&gt;"146"}, 
      "25"=&gt;{"personality_test_template_question_id"=&gt;"26", "personality_test_template_answer_id"=&gt;"152"}, 
      "26"=&gt;{"personality_test_template_question_id"=&gt;"27", "personality_test_template_answer_id"=&gt;"162"}, 
      "27"=&gt;{"personality_test_template_question_id"=&gt;"28", "personality_test_template_answer_id"=&gt;"165"}, 
      "28"=&gt;{"personality_test_template_question_id"=&gt;"29", "personality_test_template_answer_id"=&gt;"173"}, 
      "29"=&gt;{"personality_test_template_question_id"=&gt;"30", "personality_test_template_answer_id"=&gt;"177"}
    }
  }
  </code></pre>
  
  <p>I am not clear what would be the proper way to generate a hash in such format. I've tried FactoryGirl <code>attributes_for</code> method, but it doesn't generate attributes for the nested factories. Any tips will be highly appreciated.</p>
  
  <p>Thanks!</p>