Rails: _destroy attribute in a nested model skips validates_presence_of
I'm following the Nested M开发者_Python百科odel 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.
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.
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.
精彩评论