lib/mongoid/relations/referenced/many_to_many.rb in mongoid-3.0.19 vs lib/mongoid/relations/referenced/many_to_many.rb in mongoid-3.0.20
- old
+ new
@@ -28,11 +28,11 @@
docs = args.flatten
return concat(docs) if docs.size > 1
if doc = docs.first
append(doc)
base.add_to_set(foreign_key, doc.id)
- if persistable? || _creating?
+ if child_persistable?(doc)
doc.save
end
end
unsynced(base, foreign_key) and self
end
@@ -216,9 +216,26 @@
# @return [ Binding ] The binding.
#
# @since 2.0.0.rc.1
def binding
Bindings::Referenced::ManyToMany.new(base, target, metadata)
+ end
+
+ # Determine if the child document should be persisted.
+ #
+ # @api private
+ #
+ # @example Is the child persistable?
+ # relation.child_persistable?(doc)
+ #
+ # @param [ Document ] doc The document.
+ #
+ # @return [ true, false ] If the document can be persisted.
+ #
+ # @since 3.0.20
+ def child_persistable?(doc)
+ (persistable? || _creating?) &&
+ !(doc.persisted? && metadata.forced_nil_inverse?)
end
# Returns the criteria object for the target class with its documents set
# to target.
#