ActiveRecord :: ConnectionNotEstablished as part of a rake task

I am working on creating a rake task to delete multiple tables and triggers.

My rake task:

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 

I tried running it like this:

 rake remove_rubyrep RAILS_ENV=development rake remove_rubyrep 

The problem is rake error errors:

 rake aborted! ActiveRecord::ConnectionNotEstablished 

Any suggestions on how to allow the rake task to connect to the database to execute raw sql? Thanks

+7
source share
1 answer

You do not load the rails application into your rake command, so ActiveRecord never creates a database connection.

Change the rake task to:

 task :remove_rubyrep => :environment do 

After that, you will no longer need the line "establish_connection"

+21
source

All Articles