Establish connection does not work with Ruby console - ruby-on-rails

I have configured database.yml to include "logging_development" as a label for signifying another database. One of the models is using "establish_connection" to connect to the database using this "label".
My model looks like this:
class AdHistory < ActiveRecord::Base
establish_connection "logging_#{RAILS_ENV}"
The Rails server works fine when it starts and establishes connection of the model with the concerned database. But when I start ruby console and try to use the model, it uses "development" label in database.yml to establish connections. I have looked into the issue but unable to find a solution. Here is a sample of the database.yml file:
development:
adapter: mysql
encoding: utf8
reconnect: true
database: ad_production
pool: 5
username: root
password: ********
socket: /tmp/mysql.sock
logging_development:
adapter: mysql
encoding: utf8
reconnect: true
database: ad_logging
pool: 5
username: root
password: ********
socket: /tmp/mysql.sock
I am using Ruby 1.8.7 and Rails 2.3.8

Have you used the RAILS_ENV=logging_development when running the rails console? If not you should, because the default Rails environment is development. So, try to load the console using this to set the RAILS_ENV variable:
RAILS_ENV=logging_development scripts/rails console

I found the answer to my question. The problem was that I was using Multi_DB gem which was intercepting all the SQL Queries that were being executed and re-directing those queries to the slave database. That was why those queries were not being executed against the database I had chosen in the "database.yml". Once the multi_db connection is nullified, the console behaves as expected. One of the problems of using "multi_db" gem is that it only accepts names of connections as "_slave_database_". So, any deviation from the expected name for multi_db gem in database.yml file will lead to errors, and the queries will not get executed against the selected database.

Related

RoR multiple database connection in heroku

My ruby on rails app contains two database connection and its working good in localhost, but its not working on heroku. Getting this error
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.2/lib/active_record/connection_adapters/connection_specification.rb:52:in 'resolve_hash_connection': database configuration does not specify adapter (ActiveRecord::AdapterNotSpecified)
database.yml
production:
adapter: mysql2
encoding: unicode
database: first_database
pool: 5
username: root
password:
production:
adapter: mysql2
encoding: unicode
database: second_database
pool: 5
username: root
password:
Since heroku overwrites default database.yml file, I followed this tutorial https://roratmindfiresolutions.wordpress.com/2013/05/27/connect-to-remote-database-server-from-heroku to setup multiple database connection on heroku, but still no change. I am using rails 4.0.1 . Any helps and suggestions are really appreciable. Thanks.
Note that in his YAML file he calls them production and production_sec whereas you're using production twice.

Working on a rails app locally with a remote Postgres connection?

Is there a way to configure the database.yml file to connect to Heroku's Postgres remotely?
I'm having trouble understanding how Heroku, Rails and PG gem work together.
It looks like during deployment, Heroku re-writes the database.yml file - is it possible to see the contents of this updated .yml file and use it locally?
Below are the steps to access Heroku db from local development:
Login to your heroku account.
Navigate to this URL https://postgres.heroku.com/databases/ OR you can get heroku db url by running this command in the terminal: heroku pg:credentials:url
Select your application database.
You will able to see your heroku pg credentials:
Host xxxxxxxxx.77777.amazonaws.com
Database 42feddfddeee
Username 44444444444
Port xxxx
Password 777sfsadferwefsdferwefsdf
collect above details and put in your databse.yml file development evn:
adapter: postgresql
host: < host > # HOST
port: < port > # Port
database: < database name > # Database Name
username: < user_name > # User Name
password: '< password >' # Password
Restart you application,
Best of luck..!!
I am not a postgres user but this should work.
You can alter your database.yml to include host and port
production:
adapter: postgresql
encoding: unicode
database: di_production
pool: 5
username: user
password:
host: heroku.host.name
port: <postgres listen port>
And of course, you should allow connection on the server side, both at the firewall level and database level.
A simple hack to see contents of #{Rails.root}/config/database.yml is to write code to load this yml into an object, then print it out on the UI
DB_CONFIG = YAML.load(File.read("#{Rails.root}/config/database.yml", __FILE__))
puts DB_CONFIG["production"].inspect # or what ever method you want to print it
I am a bit of a newbie and may have misunderstood your question - but. Developed a ROR application using postgress database. Then uploaded to Heroku. I ran DB/Migrate/schema.rb to set up the remote Postgresql database.
From memory heroku run rake db:init (but I could be wrong). Whenver I update the database in develpment to get update in Heroku I have to promote code and run heroku run rake db:migrate
From my config/dtabase.yml
development:
adapter: postgresql
encoding: unicode
database: di_development
pool: 5
username: davidlee
password:
test:
adapter: postgresql
encoding: unicode
database: di_test
pool: 5
username: davidlee
password:
production:
adapter: postgresql
encoding: unicode
database: di_production
pool: 5
username: user
password:
and it works. I can't remember doing anything else.
Pierre

rails 3 establish_connection doesn't work for switching databases

I need to use two different db in my app. I found a simple example:
http://pullmonkey.com/2008/4/21/ruby-on-rails-multiple-database-connections/
using establish_connection, but it doesn't seem to work. To test switching databases, I have:
class User < ActiveRecord::Base
establish_connection :test
# attr_accessible :title, :body
end
But I get the error: "Uncaught exception: database configuration does not specify adapter".
Here is my database.yaml:
development:
adapter: mysql2
encoding: utf8
database: foo
username: bar
host: foo.com
password: foobar
test:
adapter: sqlite3
database: db/test.sqlite3
pool: 5
timeout: 5000
production:
adapter: sqlite3
database: db/production.sqlite3
pool: 5
timeout: 5000
I am running in development mode for this test. Any ideas why it can't switch? Thanks.
First the "pulmonkeyexample" is to use a database label when you have multiple databases and other than the three standard ones - "development, test and production". When you start a rails application, you can use "-e" option to tell rails which mode to start the application in - test, dev or prod. Depending on the mode in which the app is starting, Rails will automatically use corresponding database connections from the "database.yml" file. So remove that line from your model. On second thoughts, even if you write yet it should work provided there is a test database with the user table.

Ruby on Rails server stopped working

I am new in ror developement..i was working on a LIVE server...I just uploaded a file through sftp...after 1 day server suddenly stopped working...You can see the error message from here
it shows
There appears to be a database problem.
Your config/database.yml may not be written correctly. Please check it and fix any errors.
Your database schema may be out of date or nonexistant. Please run rake db:migrate to ensure that the database schema is up-to-date.
The database server may not be running. Please check whether it's running, and start it if it isn't.
Looking at the error page you seem to be using Rails 2.3?
At a guess you have a MySQL database not an SQLite running. You should have the user name and password for the database around somewhere (replace the relevant fields in the 3 sections with them).
Change the database names to reflect your database names.
The server admins might have set a specific socket for MySQL in which case replace the '/tmp/mysql.sock' with the socket number.
Check your Gems to see if the MySQL adapter is installed (you appear to be using Rails 2.3 so try gem list on the terminal for your server - make sure that you are in the root directory for the app).
If the MySQL gem is missing use gem install to install it (this will depend on what your hosting provider allows).
The following links are pretty old - targetted towards Rails 2 which you appear to be using.
http://www.ruby-forum.com/topic/139710
http://forums.mysql.com/read.php?116,353922,359544
database.yml
development:
adapter: mysql
encoding: utf8
database: temp_development
username: root
password:
socket: /tmp/mysql.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: mysql
encoding: utf8
database: temp_test
username: root
password:
socket: /tmp/mysql.sock
production:
adapter: mysql
encoding: utf8
database: temp_production
username: root
password:
socket: /tmp/mysql.sock

Ruby-on-rails + Postgres: configuration issue

I`m getting some troubles after installing in Windows 7 ruby 1.8.6, rails 2.3.8, some basic gems(also ruby-postgres) and the IDE Rubymine from Jetbrains.
So, after creating a simple project with Rubymine(default PostgresSQL configuration in database.yml), I run it in localhost:3000 but it seems not be recognizing nothing like:
When I first click on the main page of Ruby on Rails at "About your application’s environment"
it returns an Error: "We're sorry, but something went wrong." and even when I create a simple controller with a view and opens the right URL it tells the same problem.
I don't know if the problem is about database or something like this, but also I would like to know how to configure it in database.yml.
Default:
adapter: postgresql
encoding: unicode
database: (name of the project)_(type: test, production or development)
pool: 5
username: (name of the project)
password: (no password)
What I did:
adapter: postgresql
encoding: utf-8
database: (name of database)_(type: test, production or development)
pool: 5
username: ruby
password: (no password)
host: localhost
port: 3000
Is it right?
I don't know much about running Rails on Windows, but looking at your database.yml file above, you shouldn't be saying port: 3000. That's the default port for the rails application to run on. You need to be putting the port that PostgreSQL is running, which is typically 5432.
assuming your project is called foo, and you've created the correct databases in postgres, and postgres is listening on the default port on localhost
development:
adapter: postgresql
encoding: utf-8
database: foo_development
pool: 5
user: ruby
As stated port 3000 is where rails listens, so that will cause problems. Also you need to make sure the pga_hba.conf allows connections to the database.

Resources