Home

Disable Rake Commands in a Rails Project

Sometimes you want to disable some of the default rake tasks in a rails project. Here's a quick way to do just that.

I had a project where I wanted to limit the use of db rake tasks to keep anyone from accidentally deleting the database.

To delete a rake task, simply add this method to your Rakefile:

Rakefile

Rake.application.instance_variable_get('@tasks').delete('task')

Here task is the name of your task.

If you want to do this multiple times, you can loop over your tasks like so:

Rakefile

def remove_task(task)
Rake.application.instance_variable_get('@tasks').delete(task)
end

tasks = ['db:drop', 'db:reset', 'db:setup', 'db:rollback', 'db:seed']
tasks.each { |t| remove_task(t) }

Let's Connect

Keep Reading

How to Write a Custom Rake Task

Rake provides a great way to automate repetitive or complex tasks. Here's a look at creating a simple and a more complex task.

Apr 27, 2015

Connect to Multiple Databases in a Rake Task with Rails

Sometimes you need access to multiple database within a single rake task, for whatever reason. Here's how you do it.

Feb 17, 2015

Connect to a Remote MySQL Database in Rails

Using a remote database with rails is useful for collaborating on projects or for keeping all your data in one place. Here's how to get it set up from scratch.

Mar 09, 2015