1

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

1 Answer 1

4
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)

Sign up to request clarification or add additional context in comments.

2 Comments

Has anyone tested the solution given in this answer?
I did test it. This actually works. You should mark this answer as correct Yudho. Thanks nort.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.