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

时间:2020-12-05 20:55:18

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几乎完成了我想做的事情,但是我只想给它文件名和参数。我很确定这是可能的,因为我以前做过。有人能提醒我怎么做吗?

4 个解决方案

#1


87  

The simplest way is with rails runner because 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

In Rails 2, it's script/runner.

在Rails 2中,它是脚本/runner。

Just say rails runner script.rb

就说rails runner script.rb吧

#2


29  

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

仅仅需要环境。rb在你的脚本。如果您的脚本位于Rails应用程序的脚本目录中。

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

#3


18  

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

运行程序在Rails的上下文中运行Ruby代码。

From rails runner command:

从rails跑步者命令:

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

#4


3  

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 /任务/ 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上运行,它将在连接到生产数据库时运行。我发现这对于帮助迁移或修改表特别有用。

#1


87  

The simplest way is with rails runner because 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

In Rails 2, it's script/runner.

在Rails 2中,它是脚本/runner。

Just say rails runner script.rb

就说rails runner script.rb吧

#2


29  

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

仅仅需要环境。rb在你的脚本。如果您的脚本位于Rails应用程序的脚本目录中。

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

#3


18  

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

运行程序在Rails的上下文中运行Ruby代码。

From rails runner command:

从rails跑步者命令:

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

#4


3  

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 /任务/ 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上运行,它将在连接到生产数据库时运行。我发现这对于帮助迁移或修改表特别有用。