title: Implementing associations with Rspec/Capybara for Ruby with Rails project
body: <p>I have a model that has a method like the following:</p>
  
  <pre><code>class Post &lt; ActiveRecord::Base
      MAX_LINES = 100
      MAX_POSTS = 5
  
      def self.can_post?(user)
        user.posts.count( :conditions =&gt; ["lines &gt;= ?", MAX_LINES] ) &lt; MAX_POSTS
      end
  end
  </code></pre>
  
  <p>user has_many posts and posts belongs_to user.  I want to build tests for posts_exceeding_max_lines?. I'm following the <a href="https://github.com/thoughtbot/factory_girl/blob/master/GETTING_STARTED.md" rel="nofollow">FactoryGirl readme</a> to create a user and post factory:</p>
  
  <pre><code>FactoryGirl.define do
  
    # post factory with a `belongs_to` association for the user
    factory :post do
      title "Through the Looking Glass"
      user
    end
  
    # user factory without associated posts
    factory :user do
      name "John Doe"
  
      # user_with_posts will create post data after the user has been created
      factory :user_with_posts do
        # posts_count is declared as an ignored attribute and available in
        # attributes on the factory, as well as the callback via the evaluator
        ignore do
          posts_count 5
        end
  
        # the after(:create) yields two values; the user instance itself and the
        # evaluator, which stores all values from the factory, including ignored
        # attributes; `create_list`'s second argument is the number of records
        # to create and we make sure the user is associated properly to the post
        after(:create) do |user, evaluator|
          FactoryGirl.create_list(:post, evaluator.posts_count, user: user)
        end
      end
    end
  end
  </code></pre>
  
  <p>In my test, I create the user using:</p>
  
  <pre><code>new_posts_count = 5
  @user = FactoryGirl.create(:user_with_posts, posts_count: new_posts_count)
  </code></pre>
  
  <p>However, when I try to print out how many posts I have (with require 'pp'):</p>
  
  <pre><code>pp @user.posts.count
  </code></pre>
  
  <p>I get 0, no matter what my setting of new_posts_count. How can I change my setup so that I can get a count of 5 posts?</p>