Rails 和 mongoid:在保存/更新引用文档时对引用的文档调用验证?
我有一个引用许多用户的模型 Tracker
。 一切工作正常,但由于某些业务原因,有时我的 用户模型经常处于验证失败的情况 规则(故意)。
问题是我仍然需要同时更新我的跟踪器 那。令我惊讶的是,保存跟踪器会触发 我的用户模型的验证规则也是如此......显然然后 保存跟踪器失败。
我可以使用 save :validate => 保存我的跟踪器false
,但我不想 为此,我对跟踪器本身有特定的验证规则 我想受到尊重。 我也尝试使用停用回调但无法得到它 工作...
请帮忙!
亚历克斯
I have a model Tracker that references_many Users
.
Everything works fine, but for some business reasons, sometimes my
Users model are in a situation were often they fails the validation
rules (on purpose).
The problem is that I still need to update my Trackers in parallel of
that. And to my surprise saving the tracker will trigger the
validation rules for my User model as well... and obviously then the
saving of the tracker fails.
I could save my tracker with save :validate => false
, but I don't want
to do that I have specific validation rules on the Tracker itself that
I want to be respected.
I also tried to play with deactivating callbacks but could not get it
to work...
Help please !
Alex
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,终于弄清楚了:
references_many :referees, :class_name => “用户”,:验证=> false
这就能解决问题!
Ok, finally figured it out:
references_many :referees, :class_name => "User", :validate => false
This will do the trick !