B Seven
B Seven

Reputation: 45943

How to validate that an array is not empty in Mongoid?

class Foo
  include Mongoid::Document

  field :bars, type:Array
end

How to validate that the bars array is not empty?

Upvotes: 2

Views: 4199

Answers (1)

Ryan McGeary
Ryan McGeary

Reputation: 239885

Try the standard presence validator:

class Foo
  include Mongoid::Document

  field :bars, type: Array

  validates :bars, presence: true
end

This works because the presence validator uses the blank? method to check the attribute during validation.

Upvotes: 4

Related Questions