如何为我有点独立的 Ruby 脚本设置 Rails 环境?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/529651/
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-02 20:57:41  来源:igfitidea点击:

How can I set the Rails environment for my somewhat stand alone Ruby script?

ruby-on-railsruby

提问by Nick Messick

I have a Ruby script in my Rails app that I use to load some data from Twitter.

我的 Rails 应用程序中有一个 Ruby 脚本,用于从 Twitter 加载一些数据。

In the future I will make it an automatic background process, but for now I run it manually like:

将来我会将其设为自动后台进程,但现在我手动运行它,例如:

ruby /lib/twitter/twitterLoad.rb

In order to use the Rails model classes and such, I have the following as the top line of the script:

为了使用 Rails 模型类等,我将以下内容作为脚本的第一行:

require "#{File.dirname(__FILE__)}/../../config/environment.rb"

By default, the development environment is used. But, I'd like to be able to choose the production environment at some point.

默认使用开发环境。但是,我希望能够在某个时候选择生产环境。

Update #1: The RAILS_ENV constant is getting set in the environment.rb file. So, I was able to put ENV['RAILS_ENV'] = 'production' at the very top (before the environment.rb) line and solve my problem somewhat. So, my new question is, can do pass in env vars through the command line?

更新 #1:在 environment.rb 文件中设置 RAILS_ENV 常量。因此,我能够将 ENV['RAILS_ENV'] = 'production' 放在最顶部(在 environment.rb 之前)行并在某种程度上解决我的问题。所以,我的新问题是,可以通过命令行传入 env vars 吗?

回答by Ryan Bigg

If you're going to be using the rails environment, your best bet would be to make this a rake script. To do this, put a twitter.rakefile into lib/tasksand begin and end it like this:

如果您要使用 rails 环境,最好的办法是将其设为 rake 脚本。为此,将twitter.rake文件放入lib/tasks并像这样开始和结束它:

task(:twitter_load => :environment) do
  # your code goes here
end

That way, you're doing it by "following conventions" and it doesn't have that 'orrible smell associated with it.

那样的话,你就是通过“遵循惯例”来做的,而且它没有那种“难闻的气味”。

回答by Garrett

I currently use the following method, and I know the environment doesn't have the rb extension, it's not needed. You can also set it before running it to overwrite the ENV["RAILS_ENV"].

我目前使用以下方法,并且我知道环境没有 rb 扩展名,它不需要。您也可以在运行它之前设置它以覆盖ENV["RAILS_ENV"].

#!/usr/bin/env ruby

# Set your environment here.
ENV["RAILS_ENV"] ||= "production"

require File.dirname(__FILE__) + "/../../config/environment"

puts "Rails was loaded!"

Then to change the environment, just run it with:

然后要更改环境,只需运行它:

rb /lib/tasks/file.rb RAILS_ENV=development

回答by Mike Woodhouse

Don't forget script/runner.

不要忘记script/runner

Set your environment variable from the command line and

从命令行设置环境变量并

ruby script/runner your_script_here.rb

回答by Ryan McCuaig

The accepted answer to use rakeis well-taken, but you may still want to manually set the environment for testing utility classes.

已接受的使用答案rake很受欢迎,但您可能仍希望手动设置用于测试实用程序类的环境。

Here's what I use to set up the test environment for utility classes in /lib. For these I tend to use the Ruby convention of making my class file execute its tests when it gets run from the command line. This way I can do TDD outside of Rails' web-centric test harnesses, but still use the class within rakewithout affecting the environment that it sets.

这是我用来为/lib. 对于这些,我倾向于使用 Ruby 约定,让我的类文件在从命令行运行时执行其测试。通过这种方式,我可以在 Rails 以 Web 为中心的测试工具之外执行 TDD,但仍然可以使用内部的类,rake而不会影响它设置的环境。

This goes at the top:

这在顶部:

if (__FILE__ == 
if (__FILE__ == 
require 'bundler'
require 'bundler/setup'

ENV['RAILS_ENV'] ||= 'development'

RAILS_ROOT = Pathname.new(File.expand_path('~/path/to/root'))

puts "Loading Rails Environment from #{RAILS_ROOT}..."
require RAILS_ROOT.join('config/environment').to_s
) require 'test/unit/ui/console/testrunner' Test::Unit::UI::Console::TestRunner.run(MyClassTest) end
) ENV['RAILS_ENV'] ||= 'test' require File.join(File.dirname(__FILE__),'../config/environment.rb') end

and this goes at the bottom:

这在底部:

##代码##

回答by pantulis

You can also do

你也可以这样做

script/console development < path/to/your/script.rb

脚本/控制台开发 < path/to/your/script.rb

Admiteddly cumbersome -and will spit out lots of irb garbage after evaluating each and every line of your script- but works for quickies and you dont have to remember that damned require line.

无可否认,这很麻烦——并且会在评估脚本的每一行后吐出大量 irb 垃圾——但适用于快速且你不必记住那该死的 require 行。

And don't forget that maybe the most elegant way to extend your app with scripts that do useful things is writing Rake tasks!

并且不要忘记,使用可以做有用事情的脚本来扩展您的应用程序的最优雅的方式可能是编写 Rake 任务!

回答by Aram Verstegen

add the line: RAILS_ENV = '<your environment of choice>'

添加行: RAILS_ENV = '<your environment of choice>'

http://wiki.rubyonrails.org/rails/pages/Environments#script

http://wiki.rubyonrails.org/rails/pages/Environments#script

pantulis: It's cool that that works, but for quickies I just use RAILS_ENV = '<your environment of choice>' ruby path/to/script.rb. This is how you set environment variables in the console.

pantulis:这很酷,但对于快速,我只使用 RAILS_ENV = '<your environment of choice>' ruby path/to/script.rb。这就是您在控制台中设置环境变量的方式。

回答by Kris

##代码##

This works but only if the Gemfile of your script contains all the dependencies of your rails app. You might be able to load one Gemfile from another, e.g just evalit, to overcome this copy/paste.

这有效,但前提是脚本的 Gemfile 包含 Rails 应用程序的所有依赖项。您可能能够从另一个 Gemfile 加载一个 Gemfile,例如只是eval它,以克服这种复制/粘贴。