Rake task in Thor script ruby
i'm in the process of building an installer. And with that, i want to migrate the database somehow. I'm making my installer in Rails 3 using Thor.
So somethi开发者_如何学运维ng like(in the command line)
rake db:create
rake db:migrate
Thank you.
The rails generator api actually provides a rake method, and is very easy to use. So for example your generator file could look like:
class RakeTestGenerator < Rails::Generators::Base
source_root File.expand_path('../templates', __FILE__)
def rake_db
rake("db:migrate")
end
end
You could then execute this within your rails app by running the following.
rails g rake_test
Which would be the equivalent of running "rake db:migrate" in the command line. Note that all publicly defined methods in a rails generator are executed when the command is run.
Additional info: The rake method is provided by Rails::Generators::Actions module and is available by the Rails::Generators::Base class. See the Official Documentation for more information.
精彩评论