# Connecting to MSSQL works in IRB, but not rails

**URL:** <https://discuss.rubyonrails.org/t/connecting-to-mssql-works-in-irb-but-not-rails/2610>\
**Category:** rubyonrails-talk\
**Created:** [October 4, 2006, 5:33pm UTC](https://discuss.rubyonrails.org/t/connecting-to-mssql-works-in-irb-but-not-rails/2610 "2006-10-04T17:33:51Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![gnubbs](https://avatars.discourse-cdn.com/v4/letter/g/50afbb/32.png) [@gnubbs](https://discuss.rubyonrails.org/u/gnubbs)\
**Post date:** [October 4, 2006, 5:33pm UTC](https://discuss.rubyonrails.org/t/connecting-to-mssql-works-in-irb-but-not-rails/2610/1 "2006-10-04T17:33:51Z")

</div>

Hi,

I am attempting to get code working that connects to a MS SQL server from a Linux web server. My testing in irb works, but when I try it in rails I have no such luck. Sorry about the long post.

I have install and tested FreeTDS (works), and unixODBC (works). I have a working ODBC connector called CTEL.

I have also installed RubyDBI and RubyODBC. I tested those by starting irb1.8 and executing the following code:

require "dbi" dbh = DBI.connect('dbi:ODBC:CTEL', 'usrename', 'password') sth = dbh.execute("SELECT Org\_ID FROM Org") while row = sth.fetch do &nbsp;&nbsp;print row end sth.finish

(Where username and password are the correct values.) It works perfectly and prints out all of the Org\_IDs. So, it looks like I have all of the prereqs workings and life is good.

However, when I start working in rails things don't go as smoothly. My database.yml looks like:

ctel: &nbsp;&nbsp;adapter: sqlserver &nbsp;&nbsp;mode: odbc &nbsp;&nbsp;dsn: CTEL &nbsp;&nbsp;username: \*\*\*\*\*\*\*\*\*\*\*\* &nbsp;&nbsp;password: \*\*\*\*\*\*\*\*\*\*\*\*\*

My model is legacy\_customer.rb:

class LegacyCustomer \< ActiveRecord::Base

&nbsp;&nbsp;establish\_connection :ctel &nbsp;&nbsp;set\_table\_name :Org &nbsp;&nbsp;set\_primary\_key :Org\_ID

end

Next I open up script/console and type "test = LegacyCustomer.new" and get the following:

DBI::DatabaseError: INTERN (0) [RubyODBC]No data found &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from /usr/local/lib/site\_ruby/1.8/DBD/ODBC/ODBC.rb:186:in `=' &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from /usr/local/lib/site\_ruby/1.8/dbi/dbi.rb:733:in `=' &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from /usr/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active\_record/connection\_adapters/sqlserver\_adapter.rb:42:in `sqlserver_connection' &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from /usr/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/connection_adapters/abstract/connection_specification.rb:251:in `send' &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from /usr/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active\_record/connection\_adapters/abstract/connection\_specification.rb:251:in `connection_without_query_cache=' &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from /usr/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/query_cache.rb:54:in `connection=' &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from /usr/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active\_record/connection\_adapters/abstract/connection\_specification.rb:220:in `retrieve_connection' &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from /usr/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/connection_adapters/abstract/connection_specification.rb:78:in `connection' &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from /usr/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active\_record/base.rb:696:in `columns' &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from /usr/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/base.rb:1969:in `attributes\_from\_column\_definition' &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from /usr/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active\_record/base.rb:1351:in `initialize_without_callbacks' &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from /usr/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/callbacks.rb:236:in `initialize' &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from (irb):1:in `new' &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from (irb):1

Any ideas on what I could do from here?

Thanks for the help, Steve

---

<div class="post-metadata">

**Author:** ![gnubbs](https://avatars.discourse-cdn.com/v4/letter/g/50afbb/32.png) [@gnubbs](https://discuss.rubyonrails.org/u/gnubbs)\
**Post date:** [October 12, 2006, 3:14pm UTC](https://discuss.rubyonrails.org/t/connecting-to-mssql-works-in-irb-but-not-rails/2610/2 "2006-10-12T15:14:55Z")

</div>

Any body? I still haven't had much luck resolving this issue, so I would definitely appreciate any suggestions.

Thanks, Steve

---

<div class="post-metadata">

**Author:** ![edlitmus](https://avatars.discourse-cdn.com/v4/letter/e/e495f1/32.png) [@edlitmus](https://discuss.rubyonrails.org/u/edlitmus)\
**Post date:** [October 30, 2006, 9:13pm UTC](https://discuss.rubyonrails.org/t/connecting-to-mssql-works-in-irb-but-not-rails/2610/3 "2006-10-30T21:13:15Z")

</div>

If you have text columns that have empty strings this error will occur. It's a bug in RubyODBC. Make sure that the text columns are either NULL or have some data in them.

gnubbs wrote:
