How do I connect multiple databases in rails - ruby-on-rails

I am trying to connect multiple databases in my rails app one as a primary and the other as a secondary. This is what I have in my database.yml
# SQLite version 3.x
# gem install sqlite3
#
# Ensure the SQLite 3 gem is defined in your Gemfile
# gem 'sqlite3'
#
# development:
# adapter: postgresql
# database: martin_development
# username:
# password:
# pool: 5
# timeout: 5000
development:
adapter: sqlserver
host:
port: 1433
database:
username:
password:
I left out some data for obvious reasons but the sqlserver is connected but I lost all my data from the postgresql database and I need it as well because it holds separate data for the blog and author logins et cetera. How would I accomplish this?

Related

How to add a Postgre Database to an existing Rails project

I created a new Rails project without database (rails new myApp -O).
How can I add a Postgresql database now ?
Add the pg gem to your gemfile.
# Use postgresql as the database for Active Record
gem 'pg'
Then, if you do not have it, you will need a database.yml file in your config directory so go there and in the config directory create a fole called database.yml which should look like this.
config/database.yml
default: &default
adapter: postgresql
encoding: unicode
username: your username for your Postgresql access
password: your password for your Postgresql access
# For details on connection pooling, see rails configuration guide
# http://guides.rubyonrails.org/configuring.html#database-pooling
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
development:
<<: *default
database: The name for your **development** database that you created (probably in PGAdmin?)
test:
<<: *default
database: The name for your **test** database that you created (probably in PGAdmin?)
production:
<<: *default
database: The name for your **production** database that you created (probably in PGAdmin?)

How to connect to Postgresql from Ruby On Rails App

I am new in Ruby on rails and i am using ruby v 1.9.3 & rails 3.2.12 with window 7
i want to PostgreSQL database connection in my app.
this is my db connection
development:
adapter: postgresql
encoding: unicode
database: mmagap_development
pool: 5
timeout: 5000
host: 127.0.0.1
username: postgres
password: root
test:
adapter: postgresql
encoding: unicode
database: mmagap_test
pool: 5
username: postgres
password: root
production:
adapter: postgresql
host: 127.0.0.1
encoding: unicode
database: mmagap_production
pool: 5
username: postgres
password: root
this connection every time give error
D:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activerecord-3.2.12/lib/active_record/connection_adapters/postgresql_adapter.rb:1208:in
`initialize': received invalid response to SSL negotiation: - (PG::ConnectionBad)
Please help how to create PostgreSQL database connection in my RoR app.
You can change host: 127.0.0.1 to host: localhost. It should solve the issue.
First install pg gem
gem install pg
your apps database.yml file shoud be like this
development:
adapter: postgresql
encoding: unicode
database: myapp_development
pool: 5
username: myapp
password: password1
test:
adapter: postgresql
encoding: unicode
database: myapp_test
pool: 5
username: myapp
password: password1
Now run
rake db:setup
rake db:migrate
and after this run
rails s
Use this link to verify, Is Postgresql installed successfully or not.
DigitalOceanCommunityArticle Click Here
AskUbuntuLink Click Here
Removed host from development config in database.yml,its worked for me.
development:
adapter: postgresql
database: tes
pool: 5
timeout: 5000
username: postgres
password: root
First you should install PostgreSQL and PgAdmin
After installing this thing you will have to add some pic of code ( to connect PostgreSQL to your rails project ) in your project config/database.yml file (before add PostgreSQL code you will have to remove all Sqlite3 code from config/database.yml file).
development:
adapter: postgresql
encoding: unicode
database: database_name
pool: 5
host: localhost
username: your_PostgreSQL_username
password: your_PostgreSQL_password
test:
adapter: postgresql
encoding: unicode
database: database_name
pool: 5
username: your_PostgreSQL_username
password: your_PostgreSQL_password
staging:
url: <%= ENV['DATABASE_URL'] %>
production:
url: <%= ENV['DATABASE_URL'] %>
postgres is default username and password in PostgreSQL database\
after add this pic of code you have to add one line of code in your rails project root directory in Gemfile
gem ‘pg’
and you must have to remove sqlite3 code from Gemfile ( gem 'sqlite3', '~> 1.4' )before adding gem 'pg'

ActiveRecord connection not established

I used this command to create an app rails new guestbook
then i used :-
rails generate scaffold Person name:string
and in routes.rb i unhashed the root :to => 'people#index'
then I wrote in the address bar localhost:3000/people and I got the following error :-
ActiveRecord::ConnectionNotEstablished
ActiveRecord::ConnectionNotEstablished
Rails.root: /home/rudraksha/rbtest/gbook
please answer ASAP
Anyways, the problem is that, connection is unable to establish with database.
Have you configured you database.yml as per you database? If not you have to configure it first.
If you are using mysql, you have to install mysql gem by adding it to your gemfile and running bundle install and configure database.yml accordingly.
Example of database.yml with mysql:
development:
adapter: mysql
encoding: utf8
reconnect: false
database: <db_name>
pool: 5
username: <your_mysql_username>
password: <your_mysql_password>
host: localhost
production:
adapter: mysql
encoding: utf8
reconnect: false
database: <db_name>
pool: 5
username: <your_mysql_username>
password: <your_mysql_password>
host: localhost

How to configure multiple database sources in a rails application

I would like to configure 2 database instances for a certain environment (say staging or production). The default rails new application just provides a single database instance how do I configure 2 database instances.
You can copy and past one of your existing configurations such as development. and rename it as you want so, in the database.yml:
development:
adapter: mysql2
encoding: utf8
reconnect: false
database:
pool: 5
username: root
password:
host: localhost
new_database:
adapter: mysql2
encoding: utf8
reconnect: false
database:
pool: 5
username: root
password:
host: localhost
Then in the models you create for this new connection add the following methods to the model, for example:
class Document < ActiveRecord::Base
self.table_name = "document" # this allows you to hide a non comforming table name behind the rails model it is NOT necessary for establish_connection to work
self.establish_connection "new_database" # notice there is no = when setting this value, strange, I know.
end

Factory girl saving records in my development database

I have a very strange problem and I don't know where I should look to find it. I am developing a rails 3 app using rspec and factory girl for testing. For some reason, whenever I run any rails commands (eg to rake the db, start the dev server etc) one factory user is created and stored in my development database. The worst part is, it always has the same email, which I am validating the uniqueness of in my app, so the commands won't run until I go in manually delete the record.
I have looked all through my factories file and I don't think I am doing anything strange there, and suggestions where else I might for the code that is doing this?
EDIT: HERE IS MY database.yml
# MySQL. Versions 4.1 and 5.0 are recommended.
#
# Install the MySQL driver:
# gem install mysql2
#
# And be sure to use new-style password hashing:
# http://dev.mysql.com/doc/refman/5.0/en/old-client.html
development:
adapter: mysql2
encoding: utf8
reconnect: false
database: ATBTracking_development
pool: 5
username: [NOT TELLING]
password: [NOT TELLING]
socket: /var/run/mysqld/mysqld.sock
# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
adapter: mysql2
encoding: utf8
reconnect: false
database: ATBTracking_test
pool: 5
username: [NOT TELLING]
password: [NOT TELLING]
socket: /var/run/mysqld/mysqld.sock
production:
adapter: mysql2
encoding: utf8
reconnect: false
database: ATBTracking_production
pool: 5
username: [NOT TELLING]
password: [NOT TELLING]
socket: /var/run/mysqld/mysqld.sock
I figured it out. In my Gemfile, I had:
group :development, :test do
gem 'capybara'
gem "rspec-rails"
gem "guard-rspec"
gem "factory_girl_rails"
...
end
I moved factory girl out of this block onto its own line so it is in the test group only like this:
gem 'factory_girl_rails', :group => :test
No more problems
db/Seeds.rb maybe...but I think that only runs on db:reset and db:seed

Resources