title: FactoryGirl Has Many through
body: <p>Okay, so i've set up a many-to-many association for users, projects and the ability to watch those projects. (Called, not the best name I've ever come up with "Watchings")</p>
  
  <pre><code>#project.rb
  class Project &lt; ActiveRecord::Base
    has_many :watchings, :foreign_key =&gt; "watched_project_id"
    has_many :watchers, :through =&gt; :watchings, :source =&gt; :user 
  end
  
  #user.rb
  class User &lt; ActiveRecord::Base
    has_many :watchings
    has_many :watched_projects, :through =&gt; :watchings
  end
  
  #watching.rb
  class Watching &lt; ActiveRecord::Base
    belongs_to :user
    belongs_to :watched_project, :class_name =&gt; 'Project'
  end
  </code></pre>
  
  <p>Functionally everything is working so far. I can watch projects and find out how many watchers a project has. But my cucumber feature keeps erroring out because I don't know how to make Factory Girl cooperate.</p>
  
  <pre><code>Factory.define :user do |user|
    user.sequence(:email) {|n| "user#{n}@example.com"}
    user.password "secret"
    user.password_confirmation {|u| u.password }
  end
  
  Factory.define :project do |project|
    project.name "Project"
    project.description "Hello World"
  end
  
  Factory.define :watchings do |watching|
    watching.association(:user)
    watching.association(:watched_project)
  end
  </code></pre>
  
  <p>And my step definition:</p>
  
  <pre><code>Given /^a Project exists called "(.+)" with (\d+) watchers$/ do |project, watchers|
    @project = Factory(:project)
    watchers.to_i.times do |watcher|
      watcher = Factory(:user)
      watching = Factory(:watching)
      watching.user = watcher
      watching.watched_project = @project
    end
  end
  </code></pre>
  
  <p>Whenever the step definition runs, it panics out and says</p>
  
  <pre><code>undefined method `watching_user_id=' for #&lt;Watching:0x007ff7800c7978&gt; (NoMethodError)
  </code></pre>
  
  <p>I've tried a few different things, but I can't figure out how to make it actually make the association for the test.</p>