Method: Mongoid::Sessions::ClassMethods#with

Defined in:
lib/mongoid/sessions.rb

#with(options) ⇒ Class

Tell the next persistance operation to store in a specific collection, database or session.

Examples:

Create a document in a different collection.

Model.with(collection: "secondary").create(name: "test")

Create a document in a different database.

Model.with(database: "secondary").create(name: "test")

Create a document in a different session.

Model.with(session: "secondary").create(name: "test")

Create with a combination of options.

Model.with(session: "sharded", database: "secondary").create

Parameters:

  • options (Hash)

    The storage options.

Options Hash (options):

  • :collection (String, Symbol)

    The collection name.

  • :database (String, Symbol)

    The database name.

  • :session (String, Symbol)

    The session name.

Returns:

  • (Class)

    The model class.

Since:

  • 3.0.0



323
324
325
326
# File 'lib/mongoid/sessions.rb', line 323

def with(options)
  Threaded.set_persistence_options(self, options)
  self
end