简体   繁体   English

如何在 Rails 环境中运行 Ruby 文件?

[英]How do I run a Ruby file in a Rails environment?

I want to run a Ruby file in the context of a Rails environment.我想在 Rails 环境中运行 Ruby 文件。 rails runner almost does what I want to do, but I'd like to just give it the file name and arguments. rails runner 几乎做了我想做的事,但我只想给它文件名和 arguments。 I'm pretty sure this is possible since I've done it before.我很确定这是可能的,因为我以前做过。 Can someone remind me how to do this?有人可以提醒我该怎么做吗?

The simplest way is with rails runner because you don't need to modify your script.最简单的方法是使用rails runner ,因为您不需要修改脚本。

runner runs Ruby code in the context of Rails non-interactively. runner在 Rails 上下文中以非交互方式运行 Ruby 代码。

https://guides.rubyonrails.org/command_line.html#bin-rails-runner https://guides.rubyonrails.org/command_line.html#bin-rails-runner

Just say rails runner script.rb就说rails runner script.rb

Simply require environment.rb in your script.只需在脚本中添加environment.rb即可。 If your script is located in the script directory of your Rails app do如果您的脚本位于 Rails 应用程序的script目录中,请执行

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环境变量来控制使用的环境(开发/测试/生产)。

RAILS_ENV=production ruby script/test.rb

Runner runs Ruby code in the context of Rails non-interactively.Runner在 Rails 的上下文中以非交互方式运行 Ruby 代码。

From rails runner command:rails runner命令:

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:您还可以将 runner 用作脚本的 shebang 行,如下所示:

-------------------------------------------------------------
#!/usr/bin/env /Users/me/rails_project/script/rails runner

Product.all.each { |p| p.price *= 2 ; p.save! }
-------------------------------------------------------------

This is an old question, but in my opinion I often find it helpful to create a rake task... and it's actually very easy.这是一个老问题,但在我看来,我经常发现创建一个 rake 任务很有帮助......而且它实际上很容易。

In lib/tasks/example.rake :lib/tasks/example.rake中:

namespace :example do

desc "Sample description you'd see if you ran: 'rake --tasks' in the terminal"
task create_user: :environment do
  User.create! first_name: "Foo", last_name: "Bar"
end

And then in the terminal run:然后在终端运行:

rake example:create_user

Locally this will be run in the context of your development database, and if run on Heroku it will be run while connected to your production database.在本地,这将在您的开发数据库的上下文中运行,如果在 Heroku 上运行,它将在连接到您的生产数据库时运行。 I find this especially useful to assist with migrations, or modified tables.我发现这对于协助迁移或修改表特别有用。

You just need:您只需要:

bundle exec rails r ~/my_script.rb

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM