Today I Learned

hashrocket A Hashrocket project

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.

See More #rails TILs
Looking for help? Hashrocket has been an industry leader in Ruby on Rails since 2008. Rails is a core skill for each developer at Hashrocket, and we'd love to take a look at your project. Contact us and find out how we can help you.