项目和用户之间有很多很多关系。我的项目模型有:
has_many :project_members, dependent: :destroy, foreign_key: 'gallery_id'
has_many :members, through: :project_members,
class_name: 'User',
foreign_key: 'member_id'
用户模型:
has_many :project_members, dependent: :destroy, foreign_key: 'member_id'
has_many :member_projects, through: :project_members,
source: :member_project
和project_member模型:
belongs_to :member_project, foreign_key: 'gallery_id', class_name: 'Project'
belongs_to :member, foreign_key: 'member_id', class_name: 'User'
我把项目工厂写成:
FactoryGirl.define do
factory :project_member do
association :project
association :user
role 'owner'
end
end
然而它给了我:
> undefined method `project=' for #<ProjectMember:0x00000006f66048>
如果我写下以下内容:
FactoryGirl.define do
factory :project_member do
association :member_project, foreign_key: 'gallery_id', class_name: 'Project'
association :member, foreign_key: 'member_id', class_name: 'User'
role 'owner'
end
end
它说:
工厂未注册:member_project
我不知道如何通过工厂对象定义多对多。
答案 0 :(得分:0)
假设您有Project和User的工厂
factory :project_member do
member_project FactoryGirl.build(:project)
member FactoryGirl.build(:user)
role 'owner'
end