0

我正在尝试使用 MongoDB 作为我的数据库来构建基于 Roda 的(非 Rails)API。我安装了 Mongoid gem 并配置了连接。不幸的是,当我尝试使用创建定义的索引时rake db:mongoid:create_indexes,由于缺少索引,任务失败environmentRakefile为了提供所需的任务,我应该做什么/包括什么?

回溯:

rake aborted!
Don't know how to build task 'environment' (See the list of available tasks with `rake --tasks`)
/home/tomasz/.rvm/gems/ruby-2.7.1/bin/ruby_executable_hooks:24:in `eval'
/home/tomasz/.rvm/gems/ruby-2.7.1/bin/ruby_executable_hooks:24:in `<main>'
Tasks: TOP => db:mongoid:create_indexes
(See full trace by running task with --trace)

我的相关部分Rakefile

require 'mongoid'
path = Gem::Specification.find_by_name('mongoid')
load "#{path.gem_dir}/lib/mongoid/railties/database.rake"

rake -T输出:

rake db:create_indexes                    # Create indexes specified in Mongoid models
rake db:drop                              # Drops all the collections for the database for the current Rails.env
rake db:mongoid:create_indexes            # Create indexes specified in Mongoid models
rake db:mongoid:drop                      # Drop the database of the default Mongoid client
rake db:mongoid:purge                     # Drop all non-system collections
rake db:mongoid:remove_indexes            # Remove indexes specified in Mongoid models
rake db:mongoid:remove_undefined_indexes  # Remove indexes that exist in the database but are not specified in Mongoid models
rake db:mongoid:shard_collections         # Shard collections with shard keys specified in Mongoid models
rake db:purge                             # Drop all collections except the system collections
rake db:remove_indexes                    # Remove indexes specified in Mongoid models
rake db:reset                             # Delete data and loads the seeds
rake db:seed                              # Load the seed data from db/seeds.rb
rake db:setup                             # Create the database, and initialize with the seed data
rake db:shard_collections                 # Shard collections with shard keys specified in Mongoid models
4

1 回答 1

0

你需要:

  • 定义一个名为 的任务environment
  • 在此任务中加载所有模型类。

如何加载模型类取决于您的应用程序。

于 2020-09-20T17:53:43.220 回答