Topic: ActiveRecord::ConnectionNotEstablished for SQL Server 2008 connection

Hello, I'm new to the Rails community and I'm trying to figure out how to connect to a SQL Server database. Everything I knew up to this point has been ASP.NET, so this is quite different.

Anyway, I think I've configured my database.yml file correctly, and I've created a login with the correct username and password.  I have set up the scaffolding and placed my User.rb model in the models directory. I believe I am doing everything correctly, but I just can't seem to make it work.

Can anyone help? Thank you.

database.yml

development:
  adapter: sqlserver
  database: gsbridge
  dataserver: localhost\SQLEXPRESS
  username: gsbridge
  password: yrahcaz

# 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

models\User.rb

class User < ActiveRecord::Base
  set_primary_key "ID"
end

Re: ActiveRecord::ConnectionNotEstablished for SQL Server 2008 connection

Post what errors you are getting.

Joe got a job, on the day shift, at the Utility Muffin Research Kitchen, arrogantly twisting the sterile canvas snout of a fully charged icing anointment utensil.

Re: ActiveRecord::ConnectionNotEstablished for SQL Server 2008 connection

The web page returned by the server displays only the "ActiveRecord::ConnectionNotEstablished" error.  I'm not seeing any other errors on that page, nor can I find any others on the server console output.

Here is that output, though.

c:\Sites\gsbridge>rails s
=> Booting WEBrick
=> Rails 3.1.1 application starting in development on http://0.0.0.0:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server
[2011-12-19 19:01:02] INFO  WEBrick 1.3.1
[2011-12-19 19:01:02] INFO  ruby 1.9.2 (2011-07-09) [i386-mingw32]
[2011-12-19 19:01:02] INFO  WEBrick::HTTPServer#start: pid=12428 port=3000


Started GET "/users/zaccrites" for 127.0.0.1 at 2011-12-19 19:24:21 -0600

ActiveRecord::ConnectionNotEstablished (ActiveRecord::ConnectionNotEstablished):



Rendered C:/RailsInstaller/Ruby1.9.2/lib/ruby/gems/1.9.1/gems/actionpack-3.1.1/l
ib/action_dispatch/middleware/templates/rescues/_trace.erb (1.0ms)
Rendered C:/RailsInstaller/Ruby1.9.2/lib/ruby/gems/1.9.1/gems/actionpack-3.1.1/l
ib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (1.0ms
)
Rendered C:/RailsInstaller/Ruby1.9.2/lib/ruby/gems/1.9.1/gems/actionpack-3.1.1/l
ib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/l
ayout (11.0ms)

Re: ActiveRecord::ConnectionNotEstablished for SQL Server 2008 connection

You are missing some basics in the database.yml.   You have dataserver instead of host???   Your database.yml should look something like:

adapter: sqlserver
mode: odbc
dsn: DSN_NAME
host: localhost
database: App_development
username: uname
password: password

You'd need to setup an ODBC datasource on the windows box named DSN_NAME or whatever you decide to call it.

There are several methods for connecting to SQLServer.    If you are just going to play around on windows with SQLServer , running WEBbrick on a windows box, that's pretty straight forward.

If you plan on putting your APP into production, hosted on a Unix box, then you have more work to do. I use the ODBC approach,  and the freeTDS stuff http://www.freetds.org/userguide/..  Unix doesn't have the core protocol TDS needed to connect to SQLServer,  you have to add it your self,  freeTDS is, well, a free version of the TDS protocol!

I would NOT recommend using SQLServer as the native database, unless you are just playing around.  You'd only use freeTDS if you need to integrate to a Legacy SQLServer database.  If you eventually plan on actually putting something into production,  and don't have to deal with a legacy SQLServer database, you're probably better off getting mysql on windows straight away.

Last edited by BradHodges (2011-12-20 12:31:45)

Joe got a job, on the day shift, at the Utility Muffin Research Kitchen, arrogantly twisting the sterile canvas snout of a fully charged icing anointment utensil.

Re: ActiveRecord::ConnectionNotEstablished for SQL Server 2008 connection

I am also new, trying to connect to sql in the same environment.  Using ODBC:

development:
  adapter: sqlserver
  mode: odbc
  dsn: DSN_NAME
  host: localhost
  database: Test
  username: sa
  password: XXX

When i watch the development log, i am getting the same error with no usefull description.


ActiveRecord::ConnectionNotEstablished (ActiveRecord::ConnectionNotEstablished):
  activerecord (3.2.1) lib/active_record/connection_adapters/abstract/connection_pool.rb:374:in `retrieve_connection'
  activerecord (3.2.1) lib/active_record/connection_adapters/abstract/connection_specification.rb:168:in `retrieve_connection'
  activerecord (3.2.1) lib/active_record/connection_adapters/abstract/connection_specification.rb:142:in `connection'
  activerecord (3.2.1) lib/active_record/query_cache.rb:67:in `rescue in call'
  activerecord (3.2.1) lib/active_record/query_cache.rb:61:in `call'
  activerecord (3.2.1) lib/active_record/connection_adapters/abstract/connection_pool.rb:443:in `call'
  actionpack (3.2.1) lib/action_dispatch/middleware/callbacks.rb:28:in `block in call'
  activesupport (3.2.1) lib/active_support/callbacks.rb:405:in `_run__703957802__call__427515214__callbacks'
  activesupport (3.2.1) lib/active_support/callbacks.rb:405:in `__run_callback'
  activesupport (3.2.1) lib/active_support/callbacks.rb:385:in `_run_call_callbacks'
  activesupport (3.2.1) lib/active_support/callbacks.rb:81:in `run_callbacks'
  actionpack (3.2.1) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
  actionpack (3.2.1) lib/action_dispatch/middleware/reloader.rb:65:in `call'
  actionpack (3.2.1) lib/action_dispatch/middleware/remote_ip.rb:31:in `call'
  actionpack (3.2.1) lib/action_dispatch/middleware/debug_exceptions.rb:16:in `call'
  actionpack (3.2.1) lib/action_dispatch/middleware/show_exceptions.rb:56:in `call'
  railties (3.2.1) lib/rails/rack/logger.rb:26:in `call_app'
  railties (3.2.1) lib/rails/rack/logger.rb:16:in `call'
  actionpack (3.2.1) lib/action_dispatch/middleware/request_id.rb:22:in `call'
  rack (1.4.1) lib/rack/methodoverride.rb:21:in `call'
  rack (1.4.1) lib/rack/runtime.rb:17:in `call'
  activesupport (3.2.1) lib/active_support/cache/strategy/local_cache.rb:72:in `call'
  rack (1.4.1) lib/rack/lock.rb:15:in `call'
  actionpack (3.2.1) lib/action_dispatch/middleware/static.rb:53:in `call'
  railties (3.2.1) lib/rails/engine.rb:479:in `call'
  railties (3.2.1) lib/rails/application.rb:220:in `call'
  rack (1.4.1) lib/rack/content_length.rb:14:in `call'
  railties (3.2.1) lib/rails/rack/log_tailer.rb:14:in `call'
  rack (1.4.1) lib/rack/handler/webrick.rb:59:in `service'
  C:/RailsInstaller/Ruby1.9.3/lib/ruby/1.9.1/webrick/httpserver.rb:138:in `service'
  C:/RailsInstaller/Ruby1.9.3/lib/ruby/1.9.1/webrick/httpserver.rb:94:in `run'
  C:/RailsInstaller/Ruby1.9.3/lib/ruby/1.9.1/webrick/server.rb:191:in `block in start_thread'


Any ideas?