lib/mongoid/relations/referenced/many_to_many.rb in mongoid-2.0.0.rc.3 vs lib/mongoid/relations/referenced/many_to_many.rb in mongoid-2.0.0.rc.4
- old
+ new
@@ -23,11 +23,11 @@
# @option options [ true, false ] :continue Continue binding the
# inverse?
#
# @since 2.0.0.rc.1
def bind(options = {})
- binding.bind(options)
+ loaded and binding.bind(options)
target.map(&:save) if base.persisted? && !options[:building]
end
# Clear the relation. Will delete the documents from the db if they are
# already persisted.
@@ -171,12 +171,20 @@
# inverse?
#
# @return [ Many ] The relation.
#
# @since 2.0.0.rc.1
- def substitute(target, options = {})
- tap { target ? (@target = target.to_a; bind(options)) : (@target = unbind(options)) }
+ def substitute(new_target, options = {})
+ tap do |relation|
+ if new_target
+ binding.unbind(options)
+ relation.target = new_target.to_a
+ bind(options)
+ else
+ relation.target = unbind(options)
+ end
+ end
end
# Unbinds the base object to the inverse of the relation. This occurs
# when setting a side of the relation to nil.
#
@@ -209,10 +217,10 @@
# @param [ Document ] document The document to append to the target.
#
# @since 2.0.0.rc.1
def append(document, options = {})
loaded and target.push(document)
- metadatafy(document)
+ characterize_one(document)
binding.bind_one(document, options)
end
# Instantiate the binding associated with this relation.
#