# Script/console connection works, but Rails app fails with SQL Server

**URL:** <https://www.ruby-forum.com/t/script-console-connection-works-but-rails-app-fails-with-sql-server/123435>\
**Category:** Rails\
**Created:** [December 17, 2007, 7:21pm UTC](https://www.ruby-forum.com/t/script-console-connection-works-but-rails-app-fails-with-sql-server/123435 "2007-12-17T19:21:11Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![rubylover](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8c91f0/32.png) [@rubylover](https://www.ruby-forum.com/u/rubylover)\
**Post date:** [December 17, 2007, 7:21pm UTC](https://www.ruby-forum.com/t/script-console-connection-works-but-rails-app-fails-with-sql-server/123435/1 "2007-12-17T19:21:11Z")

</div>

Greetings,

I have an application that connects to a remote SQL Server database  
using DBI/ODBC. If I run the application via the Rails console, I am  
able to retrieve data and work with the database just fine. If,  
however I try to access it via the Web application itself, I get the  
following error:

DBI::DatabaseError (S1000 (0) [unixODBC][FreeTDS][SQL Server]Unable to  
connect to data source):  
/usr/lib/site\_ruby/1.8/DBD/ODBC/ODBC.rb:95:in `connect' /usr/lib/site_ruby/1.8/dbi.rb:424:in`connect’  
/usr/lib/site\_ruby/1.8/dbi.rb:215:in `connect’  
…

Does anyone have any ideas why this would be the case? I’ve run out  
of things to try myself, so any help would be appreciated.

Thanks!

---

<div class="post-metadata">

**Author:** ![rubylover](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8c91f0/32.png) [@rubylover](https://www.ruby-forum.com/u/rubylover)\
**Post date:** [December 17, 2007, 10:01pm UTC](https://www.ruby-forum.com/t/script-console-connection-works-but-rails-app-fails-with-sql-server/123435/2 "2007-12-17T22:01:00Z")

</div>

To follow up on this one for the googlers, adding this as an  
initializer to my project seems to fix it.

if ENV[‘RAILS\_ENV’] == ‘production’  
ENV[‘ODBCINI’] = “/etc/odbc.ini”  
ENV[‘ODBCSYSINI’] = “/etc”  
ENV[‘FREETDSCONF’] = “/etc/freetds/freetds.conf”  
end

The problem is that mongrel\_cluster is started as root and then spawns  
mongrels as deploy- but doesn’t load environment variables
