给出以下模型:
class Question < ActiveRecord::Base
has_many :answers
end
class Answers < ActiveRecord::Base
belongs_to: question
validates :comment, presence: true
end
打电话时
question = Question.new
question.answers.build
question.valid?
valid?返回false,因为关联的答案无效。写的时候
has_many :answers, validate: false
在Question valid?回报true。
这是一个错误还是在使用has_many自动验证相关模型时需要?Rails 指南明确解释了validate_associatedwithhas_many关系的使用:http: //guides.rubyonrails.org/active_record_validations_callbacks.html#validates_associated