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

前端 未结 4 1347
名媛妹妹
名媛妹妹 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:28

    Runner runs Ruby code in the context of Rails non-interactively.

    From rails runner command:

    Usage: runner [options] ('Some.ruby(code)' or a filename)
    
        -e, --environment=name           Specifies the environment for the runner to operate under (test/development/production).
                                         Default: development
    
        -h, --help                       Show this help message.
    

    You can also use runner as a shebang line for your scripts like this:

    -------------------------------------------------------------
    #!/usr/bin/env /Users/me/rails_project/script/rails runner
    
    Product.all.each { |p| p.price *= 2 ; p.save! }
    -------------------------------------------------------------
    

提交回复
热议问题