Module: Mongoid::Association::Constrainable

Included in:
Relatable
Defined in:
build/mongoid-master/lib/mongoid/association/constrainable.rb

Overview

Used for converting foreign key values to the correct type based on the types of ids that the document stores.

Instance Method Summary collapse

Instance Method Details

#convert_to_foreign_key(object) ⇒ Object

Convert the supplied object to the appropriate type to set as the foreign key for an association.

Examples:

Convert the object.

constraint.convert("12345")

Parameters:

  • object (Object)

    The object to convert.

Returns:

  • (Object)

    The object cast to the correct type.

Since:

  • 2.0.0.rc.7



23
24
25
26
27
28
29
30
31
32
33
# File 'build/mongoid-master/lib/mongoid/association/constrainable.rb', line 23

def convert_to_foreign_key(object)
  return convert_polymorphic(object) if polymorphic?
  field = relation_class.fields["_id"]
  if relation_class.using_object_ids?
    BSON::ObjectId.mongoize(object)
  elsif object.is_a?(::Array)
    object.map!{ |obj| field.mongoize(obj) }
  else
    field.mongoize(object)
  end
end