Question

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.

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top