Ruby独立脚本中的RoR环境 [英] RoR environment in Ruby standalone script
问题描述
我想运行一个独立的ruby脚本,在其中我需要使用RoR环境.具体来说,我需要扩展ActionMailer和ActiveRecord的模型.我还需要从我的database.yml中读取数据库配置. 我该怎么办?
I want to run a standalone ruby script in which I need my RoR environment to be used. Specifically, I need my models extending ActionMailer and ActiveRecord. I also need to read the database configuration from my database.yml. How do I go about it?
推荐答案
最简单的方法是从更改脚本的shebang:
The easiest way is to change the shebang of your script from :
#!/usr/bin/ruby
到
#!/path/to/your/rails/script/runner
等等,您的脚本将在完整的Rails环境加载后运行.您还可以将脚本作为./my_script -e production
运行,以使其与生产数据库一起运行.
Et voilà, your script will be run with the full rails environment loaded. You can also run your script as ./my_script -e production
to have it run with the production database.
这篇关于Ruby独立脚本中的RoR环境的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!