我有三种模式:用户,网站和护照。
用户和网站拥有并且彼此属于许多人,而护照是带有几个额外列的连接模型。
我尝试了以下两种方法:
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
无论哪种方式,我都在使用它:
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 }
请不要问为什么有site_user和护照......这需要一段时间来解释。
谢谢!
最高
答案 0 :(得分:0)
所以,重述一下:
# 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
我认为你应该能够做类似
的事情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
然后你应该能够做到
let( :site_with_users ) { FactoryGirl.create :site_with_users }
但这些都是猜测,希望它会帮助你。
答案 1 :(得分:0)
OMFG这是愚蠢的......
切换到Factory.define:passport do | f |语法使它工作...
吹掉一小时半生产力的好方法