title: Rails Association Issues in Controller Testing using Rspec
body: <p>I'm having issues trying to access my associated model in a one-to-many relationship during testing. I don't understand why the below doesn't return the association. </p>
  
  <p>The reason I care about this problem is because I wanted to use Factory Girl to call my guardian through my_factory_girl_user.guardians.first which wasn't working. I tried to simplify by using just ActiveRecord and it still isn't behaving how I would expect.</p>
  
  <p>I'm a true Rails beginner so any advice on this problem or how to better debug it would be much appreciated!</p>
  
  <p><strong>Question: Why doesn't @target_user.guardians return anything?</strong></p>
  
  <p>User Model</p>
  
  <pre><code>class User &lt; ActiveRecord::Base
    has_many :guardians, dependent: :destroy
    ...
  end
  </code></pre>
  
  <p>Guardian Model</p>
  
  <pre><code>class Guardian &lt; ActiveRecord::Base
    belongs_to :user
    ...
  end
  </code></pre>
  
  <p>Controller Spec</p>
  
  <pre><code>describe 'PUT #update' do
  context 'when logged in and authorized' do
    let(:new_attributes){
      {level: 31}
    }
    it 'sets current guardian' do
      @target_user = User.create!(profile_name: 'MyGuardian', system: 'Xbox One', region: 'North America', password: "password", password_confirmation: "password")
      @target_guardian = Guardian.create!(level: 31, guardian_class: 'Titan', activity: 'Vault of Glass', user_id: @target_user.id)
      puts "User: #{@target_user.inspect}"
      puts "Guardian: #{@target_guardian.inspect}"
      puts "user.guardians: #{@target_user.guardians}"
      put :update, {id: @target_guardian.id, guardian: new_attributes}, {user_id: @target_user.id}
      expect(assigns(:guardian)).to eq(@target_guardian)
    end
  end
  </code></pre>
  
  <p>Output</p>
  
  <pre><code>User: #&lt;User id: 3147, email: nil, password_digest: "$2a$04$QPk3kCsKLp0IX.YmMPAPdO2gCe79mwVybHkFpcEsVkG...", created_at: "2014-12-16 02:55:57", updated_at: "2014-12-16 02:55:57", is_admin: nil, profile_name: "MyGuardian", system: "Xbox One", region: "North America"&gt;
  Guaridan: #&lt;Guardian id: 3284, created_at: "2014-12-16 02:55:57", updated_at: "2014-12-16 02:55:57", user_id: 3147, fireteam_id: nil, level: 31, guardian_class: "Titan", activity: "Vault of Glass", comment: nil&gt;
  user.guardians: 
  </code></pre>
  
  <p><strong>Answer:</strong></p>
  
  <p>Thanks Typpex for the help! My issue was that I was not refreshing the User after creating the Guardian, the solution I used was adding the below line after creating my Guardian. For more information read the answer below.</p>
  
  <pre><code>@target_user.reload
  </code></pre>