Yudho Ahmad Diponegoro
Yudho Ahmad Diponegoro

Reputation: 325

mongoid validates array has at least 1 element

We have

class Event
  include Mongoid::Document
  field :categories, type: Array, default: []
end

How to validate an event object to have at least 1 element in categories? Should I use custom validator? Thank you

Upvotes: 1

Views: 937

Answers (1)

nort
nort

Reputation: 1615

class Event
  include Mongoid::Document
  field :categories, type: Array, default: []

  validates :categories, length: { minimum: 1 }
end

Should do the trick (http://guides.rubyonrails.org/active_record_validations.html#length)

Upvotes: 4

Related Questions