Method: Mongoid::Indexable::ClassMethods#create_indexes

Defined in:
lib/mongoid/indexable.rb

#create_indexestrue

Send the actual index creation comments to the MongoDB driver

Examples:

Create the indexes for the class.

Person.create_indexes

Returns:

  • (true)

    If the operation succeeded.

Since:

  • 1.0.0



29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/mongoid/indexable.rb', line 29

def create_indexes
  return unless index_specifications
  index_specifications.each do |spec|
    key, options = spec.key, spec.options
    if database = options[:database]
      with(read: { mode: :primary }, database: database).
        collection.indexes.create_one(key, options.except(:database))
    else
      with(read: { mode: :primary }).collection.indexes.create_one(key, options)
    end
  end and true
end