STATUSES = { question: %w[answered],suggestion: %w[pending planned started completed declined],problem: %w[started solved] } valIDates :status,allow_blank: true,inclusion: { in: STATUSES[:question] },if: lambda { self.category == 'question' } valIDates :status,inclusion: { in: STATUSES[:suggestion] },if: lambda { self.category == 'suggestion' } valIDates :status,inclusion: { in: STATUSES[:problem] },if: lambda { self.category == 'problem' }
我正在使用Rails 3.
解决方法 :include:in接受lambda本身:valIDates :status,inclusion: { in: lambda { |o| STATUSES[o.category.to_sym] } }
文档:http://apidock.com/rails/ActiveModel/Validations/HelperMethods/validates_inclusion_of
总结以上是内存溢出为你收集整理的ruby-on-rails – 在Ruby类中干掉条件验证全部内容,希望文章能够帮你解决ruby-on-rails – 在Ruby类中干掉条件验证所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)