Method: Mongoid::Relations::Accessors::ClassMethods#setter

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

#setter(name, metadata) ⇒ Class

Defines the setter for the relation. This does a few things based on some conditions. If there is an existing association, a target substitution will take place, otherwise a new relation will be created with the supplied target.

Examples:

Set up the setter for the relation.

Person.setter("addresses", )

Since:

  • 2.0.0.rc.1



261
262
263
264
265
266
267
268
269
270
271
272
273
274
# File 'lib/mongoid/relations/accessors.rb', line 261

def setter(name, )
  re_define_method("#{name}=") do |object|
    without_autobuild do
      if .many?
        set_relation(name, get_relation(name, ).substitute(object.substitutable))
      elsif value = get_relation_for_set(name, , object)
        set_relation(name, value.substitute(object.substitutable))
      else
        __build__(name, object.substitutable, )
      end
    end
  end
  self
end