简体   繁体   中英

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 something 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM