简体   繁体   English

如何获得独立的ActiveRecord :: Base工具连接到生产数据库?

[英]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. 我将创建一个特殊的耙任务。

  1. 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 
  2. 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.

相关问题 如何为我独立的Ruby脚本设置Rails环境? - How can I set the Rails environment for my somewhat stand alone Ruby script? 如何永久忽略ActiveRecord :: Base类中的数据库列? - How can I permanently ignore a database column in my ActiveRecord::Base class? 如何覆盖gem添加到ActiveRecord :: Base的类方法(在我的装饰器中) - How can I override a class method that a gem is adding to ActiveRecord::Base (in my decorator) 如何在Ruby on Rails中将生产数据库复制到测试数据库? - How can I copy my production-database to my test-database in Ruby on Rails? 如何在生产数据库而不是开发数据库上运行迁移? - How can I run a migration on my production database but not my development database? 如何防止在生产环境中的日志文件中单独记录某个路由 - How do I prevent logging a certain route alone in my log files in production environment 如何确定我的ActiveRecord对象是否违反了唯一的数据库密钥/索引? - How can I determine if my ActiveRecord object violates a unique database key/index? 我的生产数据库缺少一些列。 如何安全地还原它们? - My production database is missing some columns. How can I safely restore them? 如何避免设置中出现ActiveRecord :: HasManyThroughCantAssociateThroughHasOneOrManyReflection错误? - How can I avoid ActiveRecord::HasManyThroughCantAssociateThroughHasOneOrManyReflection errors in my setup? 如何将ActiveRecord查询限制为数组中的ALL? - How can I restrict my ActiveRecord query to ALL in array?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM