工厂女孩 - 给定项目,如何授予权限?协会?
我有以下模型:
Users (id)
Project (id, name)
Roles (id, title)
Permissions (user_id, role_id, project_id)
对于 Factory Girl,我想创建一个工厂,其中包括 2 个用户、1 个项目以及将 2 个用户与该项目关联的权限。
这是我到目前为止所得到的:
Factory.define :user do |user|
user.fname "James"
user.lname "Bond"
user.email "[email protected]"
user.password "password"
user.password_confirmation "password"
end
Factory.define :project do |project|
project.name "myproject"
project.private_email "myproject"
project.user_id do
(User.find_by_email('[email protected]')).id
end
project.instance_id do
(User.find_by_email('[email protected]')).instance_id
end
project.permissions {|permissions| [permissions.association(:permission)] }
end
Factory.define :permission do |permission|
permission.role_id 1
permission.creator_id do
(User.find_by_email('[email protected]')).id
end
permission.user_id do
(User.find_by_email('[email protected]')).id
end
permission.project_id do
(Project.find_by_name('myProject')).id
end
end
但是这个错误是:
失败/错误:@project = Factory(:project) 运行时错误: 为 nil 调用 id,这会被错误地设置为 4——如果你真的想要 nil 的 id,请使用 object_id
我是工厂女孩的新手,我这样做对吗?谢谢
I have the following models:
Users (id)
Project (id, name)
Roles (id, title)
Permissions (user_id, role_id, project_id)
With Factory Girl I want to make a factory which includes 2 users, 1 project, and permissions associating the 2 users to the project.
Here's what I have so far:
Factory.define :user do |user|
user.fname "James"
user.lname "Bond"
user.email "[email protected]"
user.password "password"
user.password_confirmation "password"
end
Factory.define :project do |project|
project.name "myproject"
project.private_email "myproject"
project.user_id do
(User.find_by_email('[email protected]')).id
end
project.instance_id do
(User.find_by_email('[email protected]')).instance_id
end
project.permissions {|permissions| [permissions.association(:permission)] }
end
Factory.define :permission do |permission|
permission.role_id 1
permission.creator_id do
(User.find_by_email('[email protected]')).id
end
permission.user_id do
(User.find_by_email('[email protected]')).id
end
permission.project_id do
(Project.find_by_name('myProject')).id
end
end
But this errors with:
Failure/Error: @project = Factory(:project)
RuntimeError:
Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id
I'm new to factory girl, am I doing this right? Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您希望利用关联方法将模型与另一个模型关联起来。
你会做类似的事情:
等等。
一个例子:
你会做这样的事情,例如:
我建议阅读 FactoryGirl 的 github 页面:
https://github.com/thoughtbot/factory_girl
You want to take advantage of the association method to associate models with another.
You would do something like:
etc.
An example of this:
You would do something like this, for example:
I would suggest reading the github page for FactoryGirl:
https://github.com/thoughtbot/factory_girl