0

Trying to connect to Sql Server from Rails. Not working so far. I was advised to add this line to the Gemfile (as a fix to a known bug):

gem 'activerecord-sqlserver-adapter', git: 'https://github.com/Desarrollo-CeSPI/activerecord-sqlserver-adapter.git'

Since I have a proxy, I didn't do this but had to perform a workaround: download the zip from that link, then unzip it in the rails project in a directory called AR, in CMD I migrated inside AR and ran

gem build activerecord-sqlserver-adapter.gemspec 

After this, a gem is created, called: activerecord-sqlserver-adapter-3.2.12.gem so I typed

gem install activerecord-sqlserver-adapter-3.2.12.gem. 

Success. At this point, no reference to any adaptors in the gemfile. rails s is successful: booting WEBrick, Rails 4.1.1 starting in development on....

However, when I open localhost:3000 I see a

ActiveRecord::Connection not Established error

I should mention that the database.yml file (from config) contains:

development:
  adapter: activerecord-sqlserver-adapter
  mode: odbc
  dsn: odbc_new
  host: localhost
  database: cms
  pool: 5
  username: gst1
  password: pwd1234!@

And the cms database exists, I added that user and password to it. What is missing here? Why the connection error? Oh, I also opened Control Panel (Windows 7) - Administrative tools - set up data sources (ODBC) - and added an 'odbc_new' entry with Sql Server authentication, added the user and password there, default db: cms and ran a connectivity test before completion: success.

I don't understand though why or if I really need to create and add this odbc data source there, if I specify the user and password in the database.yml file. Can you help solve this please? Many Thanks in advance.

UPDATE: I just added adapter: sql server to my database.yml. I still get the same error though. Do I have to do something to validate the change in the database.yml file apart from saving it? Should I add something to the Gemfile also? Do I need to add the local adaptor (called activerecord-sqlserver-adapter-3.2.12.gem) also in my gemfile (taken from a local drive - instead of rubygems.org)? I feel the error could be connected to that.

Not sure if I need to do sth like this in the gem file

gem 'activerecord-sqlserver-adapter-3.2.12.gem', :path => 'C:\Site\simple_cms\AR'

but if I do, running

C:\Sites\simple_cms>bundle install

gives an error afterwards:

Fetching gem metadata from https://rubygems.org/...........
Fetching additional metadata from https://rubygems.org/..
Resolving dependencies...
Could not find gem 'activerecord-sqlserver-adapter-3.2.12.gem (>= 0)
x86-mingw32' in source at C:/Sites/simple_cms/AR.
Source does not contain any versions of
'activerecord-sqlserver-adapter-3.2.12.gem (>= 0) x86-mingw32'`

UPDATE- I also tried to specify the version number like this: `gem 'activerecord-sqlserver-adapter', '3.2.12', :path => 'C:\Site\simple_cms\AR' but again an error:

`C:\Users\acm>cd C:\Sites\simple_cms

C:\Sites\simple_cms>rails s
←[31mBundler could not find compatible versions for gem "activerecord":
  In snapshot (Gemfile.lock):
    activerecord (4.1.1)

  In Gemfile:
    activerecord-sqlserver-adapter (= 3.2.12) x86-mingw32 depends on
      activerecord (= 4) x86-mingw32

Running `bundle update` will rebuild your snapshot from scratch, using only
the gems in your Gemfile, which may resolve the conflict.
←[0m

Now, After I ran a bundle update, another error:

C:\Sites\simple_cms>bundle update
Fetching gem metadata from https://rubygems.org/...........
Fetching additional metadata from https://rubygems.org/..
Resolving dependencies...
Bundler could not find compatible versions for gem "activesupport":
  In Gemfile:
    activerecord-sqlserver-adapter (= 3.2.12) x86-mingw32 depends on
      activerecord (= 4) x86-mingw32 depends on
        activesupport (= 4.0.0) x86-mingw32

    rails (= 4.1.1) x86-mingw32 depends on
      activesupport (4.1.1)
Samy
  • 465
  • 1
  • 10
  • 25
  • 1
    try adding `port: 3000` to your yaml – Uri Agassi May 15 '14 at 07:39
  • 1
    Try starting `rails console` from a terminal window, it probably will provide some more info on what is going on. – Patru May 15 '14 at 07:46
  • Not using `SQLServer` myself I can only provide minimal support, but judging from the [adapter docs](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter) you can do some special things to `Configure Connection`. But you probably looked at this already? – Patru May 15 '14 at 07:51
  • Thanks a lot Uri! Same result, though. I'm guessing it has more to do with the dsn and how or whether I write the adapter at all in database.yml. – Samy May 15 '14 at 08:16
  • Thanks a lot for the tip Patru : `C:\Sites\simple_cms>rails console C:/Ruby193/lib/ruby/gems/1.9.1/gems/activesupport-4.1.1/lib/active_support/dependencies.r b:247:in `require': Could not load 'active_record/connection_adapters/activerecord-sqlser ver-adapter_adapter'. Make sure that the adapter in config/database.yml is valid. If you use an adapter other than 'mysql', 'mysql2', 'postgresql' or 'sqlite3' add the necessary adapter gem to the Gemfile. (LoadError)` – Samy May 15 '14 at 08:18
  • 2
    Try with just `sqlserver` as the adapter in your `database.yml` –  May 15 '14 at 08:42
  • @Samy Long code blocks in comments just look garbled. Update your question instead, things will look al lot cleaner there (and be sure to mark the sections with **Update 1**, **Upddate 2** and the like). – Patru May 16 '14 at 03:25
  • Thanks, everyone. Patru, I just updated my question. Appreciate the feedback! – Samy May 16 '14 at 14:23

1 Answers1

1

Checking out this question bundler seems to be picky about the gems it tries to install from a local :path. Could you try specifying the version number as

gem 'activerecord-sqlserver-adapter', '3.2.12', :path => 'C:\Site\simple_cms\AR'

I do not think your load path will be up to snuff if bundler thinks it cannot resolve this gem.

Community
  • 1
  • 1
Patru
  • 4,481
  • 2
  • 32
  • 42
  • Thank you so much, Patru, for this. I provided another update in the original question. Unfortunately it seems to be really hard to make this work with Sql Server. The error message seems to imply that I need a previous rails version?? Not sure. I have 4.1.1. But if I install rails 4.0.0 and remove the 4.1.1 I get the 4.1.1 again automatically when running bundle install. At a loss right now. – Samy May 21 '14 at 10:32
  • If that happens you should fix your rails version in your `Gemfile`, I guess you should `gem 'rails', '4.0.0'` instead of just `gem 'rails'` before you `bundle install` – Patru May 21 '14 at 10:38