Ruby on rails ActiveRecord::ConnectionNote已在rake任务中建立
我正在创建一个rake任务来删除一些表和触发器 我的rake任务:Ruby on rails ActiveRecord::ConnectionNote已在rake任务中建立,ruby-on-rails,ruby-on-rails-3,postgresql,postgresql-9.1,Ruby On Rails,Ruby On Rails 3,Postgresql,Postgresql 9.1,我正在创建一个rake任务来删除一些表和触发器 我的rake任务: task :remove_rubyrep do sql = <<-SQL DROP TABLE rr_logged_events, rr_running_flags, rr_pending_changes; SQL ActiveRecord::Base.establish_connection ActiveRecord::Base.connection.execute(sql) end 问题
task :remove_rubyrep do
sql = <<-SQL
DROP TABLE rr_logged_events, rr_running_flags, rr_pending_changes;
SQL
ActiveRecord::Base.establish_connection
ActiveRecord::Base.connection.execute(sql)
end
问题在于rake任务错误包括:
rake aborted!
ActiveRecord::ConnectionNotEstablished
关于如何允许rake任务连接到DB以执行原始sql,有什么建议吗?感谢您没有在rake任务中加载rails应用程序,因此ActiveRecord永远不会创建数据库连接 将您的rake任务更改为:
task :remove_rubyrep => :environment do
完成此操作后,您将不再需要“建立连接”线路
task :remove_rubyrep => :environment do