[英]Rails Validation For Nested Models
I'm using Rails 3.2 and 4. 我正在使用Rails 3.2和4。
I have a Survey and I want to validate that each question has between 2 and 5 answers. 我进行了一项调查,我想确认每个问题都有2到5个答案。 Survey has_many Questions, Questions has_many Answers and belongs to Survey. 调查has_many问题,问题has_many答案,属于Survey。 Answer belongs to Question. 答案属于问题。
How would I do this? 我该怎么做?
In Question model 问题模型
validate :answer_count
def answer_count
self.errors[:base] << "Can have answers only between 2 and 5" if answers.length<1 || answers.length>5
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.