title: Factory Girl associated object instantiation [using devise]
body: <p>I have three models user (author), which is incorporating devise logic:</p>
  
  <h1>app/models/user.rb</h1>
  
  <pre><code>class User &lt; ActiveRecord::Base
    # Include default devise modules. Others available are:
    # :confirmable, :lockable, :timeoutable and :omniauthable
    devise :database_authenticatable, :registerable,
           :recoverable, :rememberable, :trackable, :validatable
  
    has_many :questions
    has_many :answers
  end
  </code></pre>
  
  <p>question:</p>
  
  <h1>app/models/question.rb</h1>
  
  <pre><code># Model for Question
  class Question &lt; ActiveRecord::Base
    has_many :answers, dependent: :destroy
    belongs_to :author, class_name: 'User', foreign_key: 'user_id'
  
    validates :title, presence: true, length: { maximum: 100 }
    validates :body, presence: true, length: { minimum: 10 }
    validates :author, presence: true
  end
  </code></pre>
  
  <p>and answer:</p>
  
  <h1>app/models/answer.rb</h1>
  
  <pre><code># Model for Answer
  class Answer &lt; ActiveRecord::Base
    belongs_to :question
    belongs_to :author, class_name: 'User', foreign_key: 'user_id'
  
    validates :body, presence: true, length: { minimum: 10 }
    validates :question_id, presence: true
    validates :author, presence: true
  end
  </code></pre>
  
  <p>and their factories:</p>
  
  <h1>spec/factories/users.rb</h1>
  
  <pre><code>FactoryGirl.define do    
    sequence :email do |n|
      "email-#{n}@example.com"
    end
  
    sequence :password do |n|
      "testpassword#{n}"
    end
  
    factory :user, aliases: [:author] do
      email
      # tried sequence generator and fixed password - both have no impact on result
      # password '1234567890'
      # password_confirmation '1234567890'
      password
    end
  
  end
  </code></pre>
  
  <h1>spec/factories/answers.rb</h1>
  
  <pre><code>FactoryGirl.define do
    factory :answer do
      body 'Answer Body'
      author
      question
    end
  
    factory :nil_answer, class: 'Answer' do
      question
      body nil
    end
  end
  </code></pre>
  
  <h1>spec/factories/questions.rb</h1>
  
  <pre><code>FactoryGirl.define do
    factory :question do
      title 'Question Title'
      body 'Question Body'
      author
  
      factory :question_with_answers  do
        after(:create) do |question|
          # changing create_list to create has no impact on result
          # create_list(:answer, 2, question: question)
          create(:answer, question: question)
        end
      end
    end
  end
  </code></pre>
  
  <p>test code:</p>
  
  <h1>spec/features/delete_answer_spec.rb</h1>
  
  <pre><code>require 'rails_helper'
  
  feature 'Delete answer', %q{
    By some reason
    As an authenticated user
    I want to delete answer
  } do
  
    given(:question) { create(:question_with_answers) }
    given(:user) { create(:user) }
    given(:ans) { create(:answer) }
  
    scenario 'Answer author password should not be nil' do
      expect(question.answers.first.author.password).to_not be_nil
      # question.author.password and ans.author.password return not nil
      # I need password to do:
      # visit new_user_session_path
      # fill_in 'Email', with: user.email
      # fill_in 'Password', with: user.password
      # click_on 'Log in'
    end 
  end
  </code></pre>
  
  <p>Can anyone explain why the following given statement:</p>
  
  <pre><code>given(:question) { create(:question_with_answers) }
  </code></pre>
  
  <p>creates question object that:</p>
  
  <pre><code>question.author.password #=&gt; '1234567890'
  </code></pre>
  
  <p>but:</p>
  
  <pre><code>question.answers.first.author.password #=&gt; nil
  </code></pre>
  
  <p>why method "create" instantiates author of question properly (field password is set), but "create_list" inside "after" callback creates author in answer with nil fields?</p>
  
  <p>rails 4.2.5, ruby 2.3.0, devise 3.5.6, warden 1.2.6, factory_girls_rails 4.6.0 (4.5.0)</p>