Rails:嵌套模型中的 _destroy 属性会跳过 validates_presence_of

发布于 2024-10-05 22:40:12 字数 591 浏览 0 评论 0原文

我正在遵循嵌套模型表单第 1 部分

但我有一个问题。

如果我添加 validates_presence_of :answers

class Question < ActiveRecord::Base
 belongs_to :survey
 validates_presence_of :answers
 has_many :answers, :dependent => :destroy
 accepts_nested_attributes_for :answers, 
        :reject_if => lambda { |a| a[:content].blank? },  
        :allow_destroy => true
end

_destroy 会跳过问题模型验证。所以我可以用空答案更新问题模型。

我可能做错了。我该如何修复它?

提前致谢。

I'm following the Nested Model Form Part 1.

But I have one problem.

If I add validates_presence_of :answers:

class Question < ActiveRecord::Base
 belongs_to :survey
 validates_presence_of :answers
 has_many :answers, :dependent => :destroy
 accepts_nested_attributes_for :answers, 
        :reject_if => lambda { |a| a[:content].blank? },  
        :allow_destroy => true
end

The _destroy skips the Question model validation. So I can update the Question model with empty answers.

I'm probably doing it wrong. How can I fix it?

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

蓝颜夕 2024-10-12 22:40:12

我认为您不想 :validates_presence_of :answers,我认为您想要 :validates_linked :answers 它将运行 Answer 的验证。

I'm thinking you do not want to :validates_presence_of :answers, I think you want :validates_associated :answers which will run Answer's validations.

静水深流 2024-10-12 22:40:12

我也问过这个问题并找到了答案。

该链接更好地描述了问题(来自我的问题)并且也有答案。
http://homeonrails.com/2012/10/validating-nested- Associations-in-rails/

基本上,他拒绝了那些标记为要销毁的内容,并计算了剩余的内容。

I've also asked this question and found the answer.

This link describes the problem better (from my question) and has the answer to it too.
http://homeonrails.com/2012/10/validating-nested-associations-in-rails/

Basically he rejected those that are marked for destruction and counted the remaining ones.

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