How do I run a Ruby file in a Rails environment?

前端 未结 4 1348
名媛妹妹
名媛妹妹 2020-12-12 18:22

I want to run a Ruby file in the context of a Rails environment. rails runner almost does what I want to do, but I\'d like to just give it the file name and arguments. I\'m

4条回答
  •  一生所求
    2020-12-12 19:29

    Simply require environment.rb in your script. If your script is located in the script directory of your Rails app do

    require File.expand_path('../../config/environment', __FILE__)
    

    You can control the environment used (development/test/production) by setting the RAILS_ENV environment variable when running the script.

    RAILS_ENV=production ruby script/test.rb
    

提交回复
热议问题