工厂女孩 - 给定项目,如何授予权限?协会?

发布于 2024-10-24 11:36:09 字数 1973 浏览 2 评论 0原文

我有以下模型:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦幻的味道 2024-10-31 11:36:09

您希望利用关联方法将模型与另一个模型关联起来。

你会做类似的事情:

 project.user {|i| i.association(:user)}

 permission.creator {|i| i.association(:user)}
 permission.project {|i| i.association(:project)}

等等。

一个例子:

Factory.define :user do |u|
  u.sequence(:email) { |n| "mike#{n}@awesome.com"}
  u.password "funnyguy1"
  u.password_confirmation {|u| u.password }
 end

Factory.define :profile do |p|
  p.sequence(:username) { |n| "madmike#{n}"}
  p.user {|i| i.association(:user)}
end

你会做这样的事情,例如:

Factory.define :permission do |permission|
  permission.role_id         1
  permission.creator {|i| i.association(:user)}
  permission.user {|i| i.association(:user)}
  permission.project {|i| i.association(:project)}
end

我建议阅读 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:

 project.user {|i| i.association(:user)}

 permission.creator {|i| i.association(:user)}
 permission.project {|i| i.association(:project)}

etc.

An example of this:

Factory.define :user do |u|
  u.sequence(:email) { |n| "mike#{n}@awesome.com"}
  u.password "funnyguy1"
  u.password_confirmation {|u| u.password }
 end

Factory.define :profile do |p|
  p.sequence(:username) { |n| "madmike#{n}"}
  p.user {|i| i.association(:user)}
end

You would do something like this, for example:

Factory.define :permission do |permission|
  permission.role_id         1
  permission.creator {|i| i.association(:user)}
  permission.user {|i| i.association(:user)}
  permission.project {|i| i.association(:project)}
end

I would suggest reading the github page for FactoryGirl:

https://github.com/thoughtbot/factory_girl

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文