创建 mongoid Rails 后跳过验证
我想在创建对象后跳过验证。让我们举个例子,
一个人有很多公司,而公司有很多人
,一个人有很多职位,而职位属于人 人员只能有一个有效展示位置
展示位置模型具有一项验证,用于检查人员在保存时是否已具有有效展示位置。
@placement is active placement
@employment.placement = @person
if @placement.save
#################
@person.placements << @placement
@company.placements << @placement
end
现在,当第一次保存展示位置时,保存它没有问题。
现在问题出现了,
@person.placements << @placement
因为该人已经通过@placement.save 进行了有效的安置。
@person.placements << @placement 再次保存 @placement 并且验证将验证错误触发到 @placement 对象。
有什么方法可以让我告诉不要在代码的 ############ 区域中的某个位置进行特定验证。
或者欢迎任何替代解决方案。
谢谢
I want to skip a validation after an object is created. Lets take an example
person has many company and company has many people
person has many placements and placement belongs to person
person can have only one active placement
Placement model has one validation that checks if a person already has an active placement when saved.
@placement is active placement
@employment.placement = @person
if @placement.save
#################
@person.placements << @placement
@company.placements << @placement
end
Now when the placement is saved for the first time, No problem its gets saved.
Now the problem comes when
@person.placements << @placement
Since the person already has active placement through @placement.save.
@person.placements << @placement again saves @placement and the validation fires validation error to @placement object.
Is there any way so that i tell not to go through that specific validation some where in ############ region of my code.
Or any alternative solutions are welcome.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用:保存:验证=>错误的
you can use: save :validate => false
如果您想在所有验证通过后保存,那么要做的第一件事是像这样
下一步是如果您使用 mongoid,则
<<
运算符调用.save
两个文件。解决方案可能是覆盖 mongoid 的
<<
,或者需要指定在哪个操作期间进行验证。验证 :placeholder, :on => :create
和或The first thing if you want to save after all the validations passes then do something like this
Next thing is if you are using mongoid then
<<
operator call.save
on both documents.The solution may be either overwrite
<<
of mongoid, Or need to speacify validation during which action.validates :placeholder, :on => :create
And Or