我制作了一个使用轨道发生器的宝石。
它会创建某些文件,包括数据库迁移。
有没有办法运行' rake db:migrate'任务自动?用户运行安装后。
或者有没有办法创造一个'问''以“运行db:migrate'”的形式Y / N?
答案 0 :(得分:1)
是的,肯定是。您只想在所述生成器的安装方法中包含该行代码。 (在下面的示例中,MyEngine和myengine代表引擎的名称)
class InstallGenerator < Rails::Generators::Base
def install
rake 'db:migrate'
end
end
但是,您需要在安装生成器中执行更多操作,而不仅仅是运行rake db:migrate。当你安装这个坏男孩时,发电机的目的是让你的生活更轻松。 为了将引擎正确安装到应用程序中,需要首先执行必需的任务。首先运行bundle install,然后安装引擎,第三次安装迁移,最后你需要运行rake db:migrate。毕竟说完了,你的引擎的安装生成器最终会看起来更像这样:
class InstallGenerator < Rails::Generators::Base
def install
run 'bundle install'
route "mount MyEngine::Engine => '/myengine'
rake 'myengine:install:migrations'
rake 'db:migrate'
end
end