我正在尝试创建一个create_indexes rake任务,用于在我的应用程序中为MongoDb记录创建索引。 我在我的应用程序的顶层目录中有Rakefile,代码如下。
require "./app"
set :database_file, "./config/mongoid.yml"
Dir.glob('lib/tasks/*.rake').each { |r| load r}
我在我的应用程序的lib / tasks文件夹中创建了一个db.rake文件
namespace :db do
task :create_indexes => :environment do
#
begin
Rails.application.eager_load!
rescue Object => e
end
#
begin
Bundler.require(:default, :assets, 'production')
rescue Object => e
end
#
to_index_models = []
ObjectSpace.each_object(Module) do |object|
begin
to_index_models.push(object) if object.respond_to?(:create_indexes)
rescue Object => e
warn "failed on: #{ object }.respond_to?(:create_indexes)"
end
end
#
to_index_models.sort! do |a, b|
begin
a.name <=> b.name
rescue Object
0
end
end
#
begin
to_index_models.uniq!
rescue Object
end
#
to_index_models.each do |model|
begin
model.create_indexes
puts "indexed: #{ model }"
rescue Object => e
warn "failed on: #{ model }#create_indexes"
end
end
end
end
我的mongoid.yml文件是localhost文件的基本连接:
development:
sessions:
default:
database: mongoid
hosts:
- localhost:27017
但每当我尝试将rake任务运行为:
rake db:create_indexes[development]
我收到以下错误:
rake aborted!
ActiveRecord::AdapterNotSpecified: database configuration does not specify adapter
/Users/harshsingh/.rvm/gems/ruby-2.1.2/gems/activerecord-4.2. 0/lib/active_record/connection_adapters/connection_specification.rb:171:in `spec'
/Users/harshsingh/.rvm/gems/ruby-2.1.2/gems/activerecord-4.2.0/lib/active_record/connection_handling.rb:50:in `establish_connection'
/Users/harshsingh/.rvm/gems/ruby-2.1.2/gems/sinatra-activerecord-2.0.4/lib/sinatra/activerecord.rb:39:in `database='
/Users/harshsingh/.rvm/gems/ruby-2.1.2/gems/sinatra-1.4.5/lib/sinatra/base.rb:1208:in `set'
/Users/harshsingh/.rvm/gems/ruby-2.1.2/gems/sinatra-activerecord-2.0.4/lib/sinatra/activerecord.rb:33:in `database_file='
/Users/harshsingh/.rvm/gems/ruby-2.1.2/gems/sinatra-1.4.5/lib/sinatra/base.rb:1208:in `set'
/Users/harshsingh/.rvm/gems/ruby-2.1.2/gems/sinatra-1.4.5/lib/sinatra/base.rb:1982:in `block (2 levels) in delegate'
/Users/harshsingh/Documents/Codes/mogreet-requestbin/Rakefile:2:in `<top (required)>'
/Users/harshsingh/.rvm/gems/ruby-2.1.2/bin/ruby_executable_hooks:15:in `eval'
/Users/harshsingh/.rvm/gems/ruby-2.1.2/bin/ruby_executable_hooks:15:in `<main>'
(See full trace by running task with --trace)