Sha256: 9c2c1f99bfbe3f6a64d8edac0b8787354aa84f0e0c91d43253c64fe5df2e1d46

Contents?: true

Size: 1.22 KB

Versions: 10

Compression:

Stored size: 1.22 KB

Contents

# encoding: utf-8
module Mongoid #:nodoc:
  module Persistence #:nodoc:
    # Insert is a persistence command responsible for taking a document that
    # has not been saved to the database and saving it. This specific class
    # handles the case when the document is embedded in another.
    #
    # The underlying query resembles the following MongoDB query:
    #
    #   collection.insert(
    #     { "_id" : 1, "field" : "value" },
    #     false
    #   );
    class InsertEmbedded < Command
      # Insert the new document in the database. If the document's parent is a
      # new record, we will call save on the parent, otherwise we will $push
      # the document onto the parent.
      #
      # Example:
      #
      # <tt>Insert.persist</tt>
      #
      # Returns:
      #
      # The +Document+, whether the insert succeeded or not.
      def persist
        parent = @document._parent
        if parent.new_record?
          parent.insert
        else
          update = { @document._inserter => { @document._position => @document.raw_attributes } }
          @collection.update(parent._selector, update, @options.merge(:multi => false))
        end
        @document.new_record = false; @document
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 5 rubygems

Version Path
mongoid-1.9.5 lib/mongoid/persistence/insert_embedded.rb
mongoid-with-auth-1.9.4 lib/mongoid/persistence/insert_embedded.rb
mongoid-rails2-1.9.4 lib/mongoid/persistence/insert_embedded.rb
mongoid-rails2-1.9.3 lib/mongoid/persistence/insert_embedded.rb
mongoid-1.9.2 lib/mongoid/persistence/insert_embedded.rb
sskirby-mongoid-1.9.1 lib/mongoid/persistence/insert_embedded.rb
mongoid-1.9.1 lib/mongoid/persistence/insert_embedded.rb
chhean-mongoid-2.0.1.beta1 lib/mongoid/persistence/insert_embedded.rb
mongoid-2.0.0.beta.5 lib/mongoid/persistence/insert_embedded.rb
mongoid-1.9.0 lib/mongoid/persistence/insert_embedded.rb