在我的模型,我有以下設置:如何使用Factory Girl中的has_many:through關係創建關聯?
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
在我factory.rb文件我有:
FactoryGirl.define do
factory :user do
sequence(:username) { |n| "user#{n}" }
email { "#{username}@example.com" }
password 'secret'
password_confirmation 'secret'
factory :admin do
...
end
end
factory :role do
name 'Normal'
value 'normal'
end
factory :assignment do
...
end
end
我竭力要弄清楚如何我想補充着,一個角色:名稱=>「管理」,:值=>「管理」,「用戶」塊裏面的「admin」的工廠,所以我可以叫
create(:admin)
在我的測試中
,並與管理員角色的用戶。
謝謝你的期待。
下面是我用來幫助我建立協會與回調的鏈接:http://robots.thoughtbot.com/post/254496652/aint-no-calla-back-girl – Nick