问题
I have three models: users, sites, and passports.
users and sites has and belongs to many each other, while passports is the join model with a couple of extra columns.
I've tried both of the following:
1.
FactoryGirl.define do
factory :passports do
association :site, factory: :site
association :user, factory: :user
access_token 'mock_token'
end
end
2.
FactoryGirl.define do
factory :passports do
access_token 'mock_token'
end
end
And either way, I'm using it with:
let(:site) { FactoryGirl.create :site }
let(:user) { FactoryGirl.create :user }
let(:site_user) { FactoryGirl.create :site_user, site: site, user: user }
let(:passport) { FactoryGirl.create :passport, user: user, site: site }
Please don't ask why there are both site_user and passport..that'll take a while to explain.
Thanks!
Max
回答1:
So, recapitulating:
# Sites
has_many :passports
has_many_and_belongs_to :users, :through => :passport
# User
has_many :passports
has_many_and_belongs to :sites, :through => :passport
# Pasport
belongs_to :site
belongs_to :user
# has a few other things going on as wel
I think you should be able to do something like
FactoryGirl.define do
factory :user do
# details go here
end
factory :site_with_users do
users { |site| [site.association(:user), site.association(:user)] }
end
end
and then you should be able to do
let( :site_with_users ) { FactoryGirl.create :site_with_users }
But these are all guesses, hope it will help you on your way.
回答2:
OMFG this is dumb...
Switching to the Factory.define :passport do |f| syntax made it work...
Great way to blow away an hour and half of productivity
来源:https://stackoverflow.com/questions/13463223/factorygirl-not-registered-only-one-factory-runs-into-this