Method: Mongoid::Fields::ClassMethods#field

Defined in:
lib/mongoid/fields.rb

#field(name, options = {}) ⇒ Field

Defines all the fields that are accessible on the Document For each field that is defined, a getter and setter will be added as an instance method to the Document.

Examples:

Define a field.

field :score, type: Integer, default: 0

Parameters:

  • name (Symbol)

    The name of the field.

  • options (Hash) (defaults to: {})

    The options to pass to the field.

Options Hash (options):

  • :type (Class | Symbol | String)

    The type of the field.

  • :label (String)

    The label for the field.

  • :default (Object | Proc)

    The field’s default.

Returns:

  • (Field)

    The generated field



483
484
485
486
487
488
489
490
491
# File 'lib/mongoid/fields.rb', line 483

def field(name, options = {})
  named = name.to_s
  Validators::Macro.validate(self, name, options)
  added = add_field(named, options)
  descendants.each do |subclass|
    subclass.add_field(named, options)
  end
  added
end