Method: Mongoid::Relations::Constraint#convert

Defined in:
lib/mongoid/relations/constraint.rb

#convert(object) ⇒ Object

Convert the supplied object to the appropriate type to set as the foreign key for a relation.

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



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/mongoid/relations/constraint.rb', line 36

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