我们有
class Event
include Mongoid::Document
field :categories, type: Array, default: []
end
如何验证事件对象在类别中至少包含1个元素?我应该使用自定义验证器吗?谢谢
答案 0 :(得分:3)
class Event
include Mongoid::Document
field :categories, type: Array, default: []
validates :categories, length: { minimum: 1 }
end
应该做的伎俩(http://guides.rubyonrails.org/active_record_validations.html#length)