如何为外部定义的多个关联编写验证?我到目前为止写的是这样的:
class Document
validate :publication_count
private
def publication_count
if publications.count > template.component_count
errors.add(:articles, 'too many')
elsif publications.count < template.component_count
errors.add(:articles, 'not enough')
end
end
publications
和template
都是关联。我刚刚收到此代码的回滚错误,即使该记录应该有效。
答案 0 :(得分:1)
您的代码显示正确,因此似乎没有正确设置或保存关联。
你检查过:
publications
和template
都已分配到Document
实例?