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")
#project.rb
class Project < ActiveRecord::Base
has_many :watchings, :foreign_key => "watched_project_id"
has_many :watchers, :through => :watchings, :source => :user
end
#user.rb
class User < ActiveRecord::Base
has_many :watchings
has_many :watched_projects, :through => :watchings
end
#watching.rb
class Watching < ActiveRecord::Base
belongs_to :user
belongs_to :watched_project, :class_name => 'Project'
end
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.
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
And my step definition:
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
Whenever the step definition runs, it panics out and says
undefined method `watching_user_id=' for #<Watching:0x007ff7800c7978> (NoMethodError)
I've tried a few different things, but I can't figure out how to make it actually make the association for the test.