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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9757261/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 03:04:08  来源:igfitidea点击:

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

ruby-on-railsrubyruby-on-rails-3

提问by weicool

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 pretty sure this is possible since I've done it before. Can someone remind me how to do this?

我想在 Rails 环境的上下文中运行 Ruby 文件。rails runner 几乎可以做我想做的事情,但我只想给它文件名和参数。我很确定这是可能的,因为我以前做过。有人可以提醒我该怎么做吗?

回答by Ryan Porter

The simplest way is with rails runnerbecause you don't need to modify your script.

最简单的方法是使用,rails runner因为您不需要修改脚本。

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

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

Just say rails runner script.rb

说啊 rails runner script.rb

回答by iltempo

Simply require environment.rbin your script. If your script is located in the scriptdirectory of your Rails app do

只需environment.rb在您的脚本中require 即可。如果您的脚本位于scriptRails 应用程序的目录中,请执行以下操作

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

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

您可以通过RAILS_ENV在运行脚本时设置环境变量来控制使用的环境(开发/测试/生产)。

RAILS_ENV=production ruby script/test.rb

回答by colsen

Runnerruns Ruby code in the context of Rails non-interactively.

Runner在 Rails 的上下文中以非交互方式运行 Ruby 代码。

From rails runnercommand:

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! }
-------------------------------------------------------------

回答by Matt

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. I find this especially useful to assist with migrations, or modified tables.

在本地,这将在您的开发数据库的上下文中运行,如果在 Heroku 上运行,它将在连接到您的生产数据库时运行。我发现这对于协助迁移或修改表特别有用。