工厂_女孩& CanCan:避免唯一性错误
我有一个带有 CanCan“角色”模型的 Rails 应用程序。角色有一个“名称”字段,cancan 调用该字段来确定用户的权限级别。
在《工厂女孩》中,我有很多与“角色”领域相关的模特;一些深度的迭代。例如:(“帐户”工厂与“购买”工厂有关联,“购买”工厂与“用户”工厂有关联,“用户”工厂与“角色”有关联)
问题是,如果我调用两个最终的工厂与“角色”关联时,调用的第二个角色将无法通过该角色 -->“名称”字段的唯一性验证。通常,唯一性验证不是问题 - 我只是将有问题的属性设置为序列...但我不能在这里这样做 - 为了 cancan 功能,角色的名称必须是特定的。
那么...我该如何解决这个问题?
I have a rails app with a CanCan 'role' model. Role has a "name" field, which cancan calls to determine a user's permission levels.
In Factory Girl I have a whole lot of models that associate with the 'Role' field; some many iterations deep. Eg: (The 'account' factory has an association with the 'purchase' factory, which has an association with the 'user' factory, which has an association with 'role')
The problem is, if I call two factories that end up associating with 'role', the second one called will fail uniqueness validation on that role-->'name' field. Normally uniqueness validations aren't an issue - I just set the problematic attribute to a sequence...but I can't do that here - the name of the role has to be specific for the sake of cancan functionality.
So...how do I get around this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以预先创建所有角色,然后在测试中根据需要查找它们吗?
如果没有,您可以创建该特定测试中所需的角色,然后将其传递到工厂吗?
Can you create all the roles up front and then just look them up as needed in the test?
If not, can you create the role you need in that specific test, and then pass it into the Factory?