[英]How can I get my stand alone ActiveRecord::Base tool to connect to my production database?
This is a script I created to add users onto my production database. 这是我创建的用于将用户添加到生产数据库中的脚本。
puts "Name of the user"
name = gets.chomp
puts "Login of the user"
login = gets.chomp
etc. etc..... 等等等等.....
What I want to do is connect to my production database so that I can easily manage new users without having to type a lot of fields multiple times. 我要做的是连接到生产数据库,这样我就可以轻松管理新用户,而不必多次键入很多字段。
At the top of the file I've tried a few ways to connect to the database but to no avail.... 在文件的顶部,我尝试了几种连接数据库的方法,但无济于事。
Attempt 1: 尝试1:
require 'config/environment.rb'
This connects me to the development database 这将我连接到开发数据库
Attempt 2: 尝试2:
require 'active_record'
ActiveRecord::Base.establish_connection(:production)
`establish_connection': production database is not configured (ActiveRecord::AdapterNotSpecified)
Attempt 3: 尝试3:
require 'active_record'
ActiveRecord::Base.establish_connection(:adapter => 'postgresql', :host => 'company_host')
load_missing_constant': uninitialized constant Rails (NameError)
Attempt 4: 尝试4:
require 'active_record'
ActiveRecord::Base.establish_connection(:adapter => 'postgresql', :host => 'company_host', :database => '/var/local/config/database.yml')
load_missing_constant': uninitialized constant Rails (NameError)
Attempt 5: 尝试5:
ActiveRecord::Base.establish_connection(:adapter => 'postgresql', :host => 'host.company.com',
:database => 'production', :user => 'user', :password => 'password')
My database.yml file entry for the production database looks like this. 我的生产数据库的database.yml文件条目如下所示。
production:
db_host: host.company.com
db_name: production
db_pass: password
db_user: user
I've tried other combinations found here API guide but to no avail. 我尝试了在此处找到的其他组合API指南,但无济于事。 I am running rails 2.3.8.
我正在运行Rails 2.3.8。 and ruby 2.0.
和ruby 2.0。 Help would be much appreciated.
帮助将不胜感激。
I would create a special rake task. 我将创建一个特殊的耙任务。
Assuming I'd want a separate namespace manual
and the name of the task is create_user
: 假设我想要一个单独的名称空间
manual
并且任务的名称为create_user
:
namespace manual task :create_user => :environment do puts "Name of the user" name = gets.chomp puts "Login of the user" login = gets.chomp end
Run ENV=production rake manual:create_user
运行
ENV=production rake manual:create_user
You could even pass parameters to the rake task, if this helps. 如果有帮助,您甚至可以将参数传递给rake任务。
Not sure if this is what you are looking for, but maybe this turns out a faster alternative for what you're trying to achieve. 不确定这是否是您要寻找的东西,但是也许这会为您要实现的目标提供更快的选择。
Go ahead and comment on the answer if this doesn't help :) 如果这没有帮助,请继续对答案发表评论:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.