Ruby-on-rails 在“生产”上运行 rake 任务并指定环境?

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

running rake task on 'production' and specifying environment?

ruby-on-railsrake

提问by timpone

I have a host at Linode and am trying to run a Rake task on it, but I get a mySQL error saying it can't connect. It looks like it thinks it is on dev. I did some Googling and saw that I can do something like this:

我在 Linode 有一个主机,我试图在它上面运行一个 Rake 任务,但是我收到一个 mySQL 错误,说它无法连接。看起来它认为它在开发中。我做了一些谷歌搜索,发现我可以做这样的事情:

bundle exec rails c

It loads the dev environment and I can't run User.allgiving me an access denied error.

它加载了开发环境,但我无法运行User.all,出现访问被拒绝错误。

If I run bundle exec rails c RAILS_ENV=productionI get the error:

如果我运行,bundle exec rails c RAILS_ENV=production我会收到错误:

Rails.env=production database is not configured (ActiveRecord::AdapterNotSpecified)

However, if I access it via the web, everything is OK. I was able to run rake db:seedbefore so I know that there's some way around this.

但是,如果我通过网络访问它,则一切正常。我以前可以跑步rake db:seed,所以我知道有办法解决这个问题。

Accessing mySQL with the production credentials works fine.

使用生产凭据访问 mySQL 工作正常。

Any ideas?

有任何想法吗?

回答by Nick Colgan

Try this:

尝试这个:

rails c production

or, at the beginning:

或者,一开始:

RAILS_ENV=production rails c

It thinks you're passing RAILS_ENV=productionas an argument when you put it at the end.

RAILS_ENV=production当你把它放在最后时,它认为你作为一个参数传递。

回答by Hunter

If you want to run your console in the context of the current bundle in your Gemfile and ensure you're using your Gemset use:

如果您想在 Gemfile 中当前包的上下文中运行控制台并确保您使用的是 Gemset,请使用:

bundle exec rails c production 

回答by Mark Locklear

This works for me. It depends on how your server and all its dependencies are set up:

这对我有用。这取决于您的服务器及其所有依赖项的设置方式:

RAILS_ENV=production bundle exec rails console

RAILS_ENV=production bundle exec rails console