How do I create an association with a has_many :through relationship in Factory Girl?
问题 In my models I have the following setup: class User < ActiveRecord::Base has_many :assignments has_many :roles, :through => :assignments end class Role < ActiveRecord::Base has_many :assignments has_many :users, :through => :assignments end class Assignment < ActiveRecord::Base belongs_to :user belongs_to :role attr_accessible :role_id, :user_id end In my factory.rb file I have: FactoryGirl.define do factory :user do sequence(:username) { |n| "user#{n}" } email { "#{username}@example.com" }