Rails:嵌套模型中的 _destroy 属性会跳过 validates_presence_of
我正在遵循嵌套模型表单第 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为您不想
: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.我也问过这个问题并找到了答案。
该链接更好地描述了问题(来自我的问题)并且也有答案。
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.