简体   繁体   中英

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:

require 'config/environment.rb'

This connects me to the development database

Attempt 2:

require 'active_record'
ActiveRecord::Base.establish_connection(:production)

`establish_connection': production database is not configured (ActiveRecord::AdapterNotSpecified)

Attempt 3:

require 'active_record'
ActiveRecord::Base.establish_connection(:adapter => 'postgresql', :host => 'company_host')

load_missing_constant': uninitialized constant Rails (NameError)

Attempt 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:

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.

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. I am running rails 2.3.8. and 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 :

     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

You could even pass parameters to the rake task, if this helps.

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 :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM