Method: Mongoid::Persistable::Maxable#set_max

Defined in:
lib/mongoid/persistable/maxable.rb

#set_max(fields) ⇒ Document Also known as: clamp_lower_bound

Set the given field or fields to the larger of either it’s current value, or a given value.

Examples:

Set a field to be no less than 100.

document.set_max(field: 100)


22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/mongoid/persistable/maxable.rb', line 22

def set_max(fields)
  prepare_atomic_operation do |ops|
    process_atomic_operations(fields) do |field, value|
      current_value = attributes[field]
      if value > current_value
        process_attribute field, value
        ops[atomic_attribute_name(field)] = value
      end
    end
    { "$max" => ops } unless ops.empty?
  end
end