Module: Mongoid::Validations::ClassMethods

Defined in:
lib/mongoid/validations.rb

Instance Method Summary collapse

Instance Method Details

#validates_relation(metadata) ⇒ Object

Adds an associated validator for the relation if the validate option was not provided or set to true.

Examples:

Set up validation.

Person.validates_relation()

Parameters:

  • metadata (Metadata)

    The relation metadata.

Since:

  • 2.0.0.rc.1



122
123
124
125
126
# File 'lib/mongoid/validations.rb', line 122

def validates_relation()
  if .validate?
    validates_associated(.name)
  end
end

#validates_with(*args, &block) ⇒ Object

Note:

See ActiveModel::Validations::With for full options. This is overridden to add autosave functionality when presence validation is added.

Add validation with the supplied validators forthe provided fields with options.

Examples:

Validate with a specific validator.

validates_with MyValidator, on: :create

Parameters:

  • *args (Class<Array>, Hash)

    The validator classes and options.

Since:

  • 3.0.0



141
142
143
144
145
146
147
148
149
150
151
# File 'lib/mongoid/validations.rb', line 141

def validates_with(*args, &block)
  if args.first == PresenceValidator
    args.last[:attributes].each do |name|
       = relations[name.to_s]
      if  && [:autosave] != false
        autosave(.merge!(autosave: true))
      end
    end
  end
  super
end

#validating_with_query?true, false

Are we currently performing a validation that has a query?

Examples:

Are we validating with a query?

Model.validating_with_query?

Returns:

  • (true, false)

    If we are validating with a query.

Since:

  • 3.0.2



161
162
163
# File 'lib/mongoid/validations.rb', line 161

def validating_with_query?
  Threaded.executing?("#{name}-validate-with-query")
end