Dmat00
Dmat00

Reputation: 417

Ruby - ActiveRecord::ConnectionNotEstablished

I am rather new to Ruby and have been following along with the book "Ruby On Rails 3 Tutorial - Learn Ruby by Example - by Michael Hartl". I am currently in Chapter 3 which discusses static pages.

In this chapter, I entered the following command into the prompt: rails generate controller Pages home contact and everything worked fine.

Then the book directs me to http://localhost:3000/pages/home. When I point my browser there, I receive the following error.

ActiveRecord::ConnectionNotEstablished

ActiveRecord::ConnectionNotEstablished
Rails.root: /home/ralph/railsprojects/sample_app

Application Trace | Framework Trace | Full Trace

The routes and controller erb don't appear to have any errors. Is this a database related error? Any ideas?

Thanks, DMAT

Update:

This is the code in my database.yml file.

# SQLite version 3.x
#   gem install sqlite3
#
#   Ensure the SQLite 3 gem is defined in your Gemfile
#   gem 'sqlite3'
development:
  adapter: sqlite3
  database: db/development.sqlite3
  pool: 5
  timeout: 5000

# 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: sqlite3
  database: db/test.sqlite3
  pool: 5
  timeout: 5000

production:
  adapter: sqlite3
  database: db/production.sqlite3
  pool: 5
  timeout: 5000

Here is the information from the Framework Trace:

  activerecord (3.1.1) lib/active_record/connection_adapters/abstract/connection_pool.rb:409:in `retrieve_connection'
  activerecord (3.1.1) lib/active_record/connection_adapters/abstract/connection_specification.rb:107:in `retrieve_connection'
  activerecord (3.1.1)lib/active_record/connection_adapters/abstract/connection_specification.rb:89:in `connection'
  activerecord (3.1.1) lib/active_record/query_cache.rb:65:in `call'
  activerecord (3.1.1) lib/active_record/connection_adapters/abstract/connection_pool.rb:477:in `call'
  actionpack (3.1.1) lib/action_dispatch/middleware/callbacks.rb:29:in `call'
  activesupport (3.1.1) lib/active_support/callbacks.rb:392:in `_run_call_callbacks'
  activesupport (3.1.1) lib/active_support/callbacks.rb:81:in `send'
  activesupport (3.1.1) lib/active_support/callbacks.rb:81:in `run_callbacks'
  actionpack (3.1.1) lib/action_dispatch/middleware/callbacks.rb:28:in `call'
  actionpack (3.1.1) lib/action_dispatch/middleware/reloader.rb:68:in `call'
  rack (1.3.5) lib/rack/sendfile.rb:101:in `call'
  actionpack (3.1.1) lib/action_dispatch/middleware/remote_ip.rb:48:in `call'
  actionpack (3.1.1) lib/action_dispatch/middleware/show_exceptions.rb:47:in `call'
  railties (3.1.1) lib/rails/rack/logger.rb:13:in `call'
  rack (1.3.5) lib/rack/methodoverride.rb:24:in `call'
  rack (1.3.5) lib/rack/runtime.rb:17:in `call'
  activesupport (3.1.1) lib/active_support/cache/strategy/local_cache.rb:72:in `call'
  rack (1.3.5) lib/rack/lock.rb:15:in `call'
  actionpack (3.1.1) lib/action_dispatch/middleware/static.rb:53:in `call'
  railties (3.1.1) lib/rails/engine.rb:456:in `call'
  railties (3.1.1) lib/rails/rack/content_length.rb:16:in `call'
  railties (3.1.1) lib/rails/rack/log_tailer.rb:14:in `call'
  rack (1.3.5) lib/rack/handler/webrick.rb:59:in `service'
  /usr/lib/ruby/1.8/webrick/httpserver.rb:104:in `service'
  /usr/lib/ruby/1.8/webrick/httpserver.rb:65:in `run'
  /usr/lib/ruby/1.8/webrick/server.rb:173:in `start_thread'
  /usr/lib/ruby/1.8/webrick/server.rb:162:in `start'
  /usr/lib/ruby/1.8/webrick/server.rb:162:in `start_thread'
  /usr/lib/ruby/1.8/webrick/server.rb:95:in `start'
  /usr/lib/ruby/1.8/webrick/server.rb:92:in `each'
  /usr/lib/ruby/1.8/webrick/server.rb:92:in `start'
  /usr/lib/ruby/1.8/webrick/server.rb:23:in `start'
  /usr/lib/ruby/1.8/webrick/server.rb:82:in `start'
  rack (1.3.5) lib/rack/handler/webrick.rb:13:in `run'
  rack (1.3.5) lib/rack/server.rb:265:in `start'
  railties (3.1.1) lib/rails/commands/server.rb:70:in `start'
  railties (3.1.1) lib/rails/commands.rb:54
  railties (3.1.1) lib/rails/commands.rb:49:in `tap'
  railties (3.1.1) lib/rails/commands.rb:49
  script/rails:6:in `require'
  script/rails:6

Upvotes: 39

Views: 73158

Answers (13)

Nezir
Nezir

Reputation: 6905

In my case I am trying wsl2 windows linux subsystem, ubuntu 16.04 I just had to add exact gem version: gem 'sqlite3', '~>1.3.13' into Gemfile and do another bundle install

Upvotes: 0

Dnyaneshwar Harer
Dnyaneshwar Harer

Reputation: 842

Below steps worked for me..

  1. create blank development.sqlite3 file in db/ folder.

  2. close current terminal and create fresh terminal

Then run rails server

Upvotes: 0

ashim888
ashim888

Reputation: 206

i am new to it but this might be pretty helpful(if you are in development just check development leave production and test environments):

  1. Create a database and give it a name.

  2. Open up .config/database.yml

    adapter: mysql2

    database: specify_your_newly_created_database #check if its automatically updated or not

    pool: 5

    username: your designated username.

    password: your designated password.

    host: localhost

  3. $ rails server

    => Booting WEBrick

    => Rails 3.2.9 application starting in development on http:// 0. 0. 0 .0 :3000

    => Call with -d to detach

    => Ctrl-C to shutdown server

    [2012-12-12 23:31:50] INFO WEBrick 1.3.1

    [2012-12-12 23:31:50] INFO ruby 1.9.3 (2012-11-10) [i386-mingw32]

    [2012-12-12 23:31:50] INFO WEBrick::HTTPServer#start: pid=4068 port=3000

    Started GET "/demo/index" for 127.0.0.1 at 2012-12-12 23:32:40 +0545

    Connecting to database specified by database.yml

    Processing by DemoController#index as HTML

    Rendered demo/index.html.erb within layouts/application (0.0ms)

    Compiled demo.css (46ms) (pid 4068)

and so on...........

if your'e just trying to solve the ActiceRecord then this will solve the problem and display your first hello page. This is the problem that occurs sometime since rails searches for the database though we have nothing to do with database when dealing with the first hello page, so just check it once if not the we will solve it together. Cheers!

Upvotes: 0

Murali
Murali

Reputation: 1

Update the adapter in config/database.yml file to the database name being used by your application and restart your rails. This will resolve the issue.

For me it got reset to some other DB name, so it was giving error. After updating it to "sqlite3", it worked fine.

Upvotes: 0

Matthew Saeed
Matthew Saeed

Reputation: 1

Using the latest sqlite3 + bundle update + bundle install worked for me. I believe the restriction to an older sqlite3 version per the walkthrough is likely messing things up. Here's my gemfile:

source 'https://rubygems.org'

# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.0.0'



# Use sqlite3 as the database for Active Record
gem 'sqlite3'


# Use SCSS for stylesheets
gem 'sass-rails', '~> 4.0.0'

# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'

# Use CoffeeScript for .js.coffee assets and views
gem 'coffee-rails', '~> 4.0.0'

# See https://github.com/sstephenson/execjs#readme for more supported runtimes
# gem 'therubyracer', platforms: :ruby

# Use jquery as the JavaScript library
gem 'jquery-rails'

# Turbolinks makes following links in your web application faster. Read more:     https://github.com/rails/turbolinks
gem 'turbolinks'

# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 1.2'

group :doc do
  # bundle exec rake doc:rails generates the API under doc/api.
  gem 'sdoc', require: false
end

# Use ActiveModel has_secure_password
# gem 'bcrypt-ruby', '~> 3.0.0'

# Use unicorn as the app server
# gem 'unicorn'

# Use Capistrano for deployment
# gem 'capistrano', group: :development

# Use debugger
# gem 'debugger', group: [:development, :test]

Upvotes: 0

Adam Loving
Adam Loving

Reputation: 443

I was migrating from a previous version of Rails. For me the fix was to add

require 'rails/all'

at the top of application.rb instead of the 5 separate requires I had.

Upvotes: 0

Sachin Maharjan
Sachin Maharjan

Reputation: 181

Add this to your Gemfile:

gem 'sqlite3-ruby', :require => 'sqlite3'

Upvotes: 0

Sooth
Sooth

Reputation: 3084

bundle install
bundle update
rake db:setup
rake db:migrate
rake db:seed
# remember to restart the server
rails server

Upvotes: 3

hao
hao

Reputation: 1214

In development environment, just run

bundle exec rake db:setup

In production environment, run

bundle exec RAILS_ENV=production rake db:setup

Upvotes: 1

freytag
freytag

Reputation: 4819

If you have in your Gemfile something like:

gem 'sqlite3-ruby', '> 1.2.0', :require => 'sqlite3'

replace it with:

gem 'sqlite3'

Upvotes: 2

buzzer123
buzzer123

Reputation: 216

I had the same problem, I started the sample-app from scratch and used this gemfile:

source 'http://rubygems.org'

gem 'rails', '3.1.1'

gem 'sqlite3'

group :development do
  gem 'rspec-rails', '2.6.1'
end


group :test do
  gem 'rspec-rails', '2.6.1'
  gem 'webrat', '0.7.1'
end

group :assets do
  gem 'sass-rails',   '~> 3.1.4'
  gem 'coffee-rails', '~> 3.1.1'
  gem 'uglifier', '>= 1.0.3'
end

gem 'jquery-rails'

This solved the problem for me :)

Upvotes: 19

Artem Kalinchuk
Artem Kalinchuk

Reputation: 6642

Try updating the sqlite3 gem. bundle update sqlite3

Upvotes: 3

nkm
nkm

Reputation: 5914

Make sure that you have installed sqlite3 gem.

If so did you run the following commands,

rake db:create # to create database
rake db:migrate # to create tables based on your migration

If the above two works fine, your application should be able to connect to the database. Else please copy the trace application trace over here, that may help us to help you better.

Upvotes: 10

Related Questions