Run a rake task from within your Rails application
I was curious if I could trigger a rake task from within my Rails application.
After some digging, I found Rails::Command.invoke()
which lets you run rake tasks from inside your Rails app.
Here's an example using db:migrate:
Rails::Command.invoke('db:migrate')
Just pass it the name of your task (db:migrate, your_custom_task, etc) and it will begin processing it.
This is great for automating or triggering rake tasks during bootstrapping or background operations without needing to leave the Rails environment.
Tweet