【发布时间】:2014-11-07 11:11:31
【问题描述】:
昨天,我疯了,因为我无法在模型中进行验证,在 Rails 4.1.6 上的 ActiveRecord 上使用 many-to-many 关系。
我尝试使用这个 gem:gem 'deferred_associations' 建议:this blog
但没有成功。
所以我扼杀了 MVC 的感觉...我在控制器中进行了验证...:/ 但是现在我改变了我的应用程序的关系和业务逻辑。 但问题仍然存在。
有人可以向我解释如何验证has_and_belongs_to_many 关系是可能的吗?
我的问候!
更新 01 到 @User089247
# My logic is simple: A question should has 3 or less answers.
# A answer can be used in many questions.
validates_associated :answers, if: :has_more_space?
private
def has_more_space?
if answers.size > 3
true
else
errors.add(:answers, "You can't do that!")
false
end
end
【问题讨论】:
标签: ruby-on-rails ruby validation activerecord