Class: Mongoid::Fields::Serializable::ForeignKeys::Object

Inherits:
Object
  • Object
show all
Includes:
Mongoid::Fields::Serializable
Defined in:
lib/mongoid/fields/serializable/foreign_keys/object.rb

Overview

Defines the behaviour for integer foreign key fields.

Instance Attribute Summary

Attributes included from Mongoid::Fields::Serializable

#default, #label, #name, #options

Instance Method Summary collapse

Methods included from Mongoid::Fields::Serializable

#cast_on_read?, #constraint, #deserialize, #eval_default, #metadata, #type, #versioned?

Instance Method Details

#object_id_field?true, false

Is the field a BSON::ObjectId?

Examples:

Is the field a BSON::ObjectId?

field.object_id_field?

Returns:

  • (true, false)

    If the field is a BSON::ObjectId.

Since:

  • 2.2.0



19
20
21
22
# File 'lib/mongoid/fields/serializable/foreign_keys/object.rb', line 19

def object_id_field?
  @object_id_field ||=
    .polymorphic? ? true : .klass.using_object_ids?
end

#serialize(object) ⇒ Array

Serialize the object from the type defined in the model to a MongoDB compatible object to store.

Examples:

Serialize the field.

field.serialize(object)

Parameters:

  • object (Object)

    The object to cast.

Returns:

  • (Array)

    The converted object.

Since:

  • 2.1.0



35
36
37
# File 'lib/mongoid/fields/serializable/foreign_keys/object.rb', line 35

def serialize(object)
  object.blank? ? nil : constraint.convert(object)
end