Ruby + Mongoid: how to make the required field?

I am using the githead version of Mongoid (due to Rails 4) and I want to make the required document with:

class MyClass
  include Mongoid::Document
  field :name,              type: String, required: true

And I have this error:

Problem: Invalid option :required provided for field :name. Summary: Mongoid requires that you only provide valid options on each field definition in order to prevent un...

What am I doing wrong?

+4
source share
1 answer

You need to use it validates_presence_of, so your class will look like this:

class MyClass
  include Mongoid::Document
  field :name, type: String
  validates_presence_of :name
end

For further documentation on checking for mongoid, you can use http://mongoid.org/en/mongoid/docs/validation.html .

+6
source

All Articles