3

Similarly to how mongoose for nodejs has an enum validator whereby the value of the field must be one of the values in a predefined array, how would a mongoid model in Rails replicate this behavior?

ie.

field :category, type: String --> must be in one of [categoryA, categoryB, categoryC]
arcyqwerty
  • 10,325
  • 4
  • 47
  • 84

1 Answers1

6

You can use this:

validates :category, :inclusion => { :in => ["categoryA", "categoryB", "categoryC"] }
Tanzeeb Khalili
  • 7,324
  • 2
  • 23
  • 27