[英]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.
我正在使用Thor在Rails 3中安装我的安装程序。
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. rails生成器api实际上提供了rake方法,并且非常易于使用。 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应用程序中执行此操作。
rails g rake_test
Which would be the equivalent of running "rake db:migrate" in the command line. 这相当于在命令行中运行“rake db:migrate”。 Note that all publicly defined methods in a rails generator are executed when the command is run.
请注意,在运行命令时,将执行rails生成器中的所有公开定义的方法。
Additional info: The rake method is provided by Rails::Generators::Actions module and is available by the Rails::Generators::Base class. 附加信息: rake方法由Rails :: Generators :: Actions模块提供,并且可以由Rails :: Generators :: Base类使用。 See the Official Documentation for more information.
有关更多信息,请参见官方文档 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.