Method: Mongoid::Tasks::Database#create_collections

Defined in:
lib/mongoid/tasks/database.rb

#create_collections(models = ::Mongoid.models, force: false) ⇒ Object

Create collections for each model given the provided globs and the class is not embedded.

Parameters:

  • models. (Array<Mongoid::Document>)

    Array of document classes for which collections should be created. Defaulted to all document classes in the application.

  • force (true | false) (defaults to: false)

    If true, the method will drop existing collections before creating new ones. If false, the method will create only new collection (that do not exist in the database).



21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/mongoid/tasks/database.rb', line 21

def create_collections(models = ::Mongoid.models, force: false)
  models.each do |model|
    if !model.embedded? || model.cyclic?
      model.create_collection(force: force)
      logger.info("MONGOID: Created collection for #{model}:")
    else
      logger.info("MONGOID: collection options ignored on: #{model}, please define in the root model.")
    end
  rescue Exception
    logger.error "error while creating collection for #{model}"
    raise
  end
end