First of all many thanks to everybody contributing to solve this
problem of mine, especially Nick S., who proposed what ended up
being a pretty easy way to making everything work.
The original task: I needed to be able to use SQL Server 2005 in a RoR
application in a Windows environment.
The problem: Accessing SQL Server from RoR was proving a pretty
challenging task for my little and lazy brain.
The solution: Use JRuby and activerecord-jdbcmssql-adapter, as Nick
suggested.
Here are the steps:
- Install JRuby.
Go to Downloads — JRuby.org and select the version you
need (in my case it was “JRuby 1.5.0 Windows Executable
(md5, sha1)”), then run the executable.
- Get familiar with how to run JRuby commands
Go to http://kenai.com/projects/jruby/pages/GettingStarted and
check out the several commands on that page. The commands
are similar to Ruby’s but different enough that it’s a good idea
to
check them out. Pay especial attention to ‘jruby -S’
- Install activerecord-jdbcmssql-adapter
At the command line type:
=> jruby -S gem install activerecord-jdbcmssql-adapter
- Install rails (this one is only for those who are as thick as I am,
probably nobody but me)
What? Again? I already have the thing installed, don’t I?
No, you don’t. It took me a moment to realize this but you are
installing rails under JRuby. The other version you have runs
under regular Ruby (one of Nick’s comments light the bulb in my
head).
=> jruby -S gem install rails (in my case: jruby -S gem install
rails -v=2.3.5)
- Create your rails application
=> jruby -S rails your_app
- Run the jdbc generator (as per Nick’s instructions).
=> jruby script/generate jdbc
- Your database.yml should look something like this:
development:
adapter: mssql
database: your_db_name_here
username: your_user_name_here
password: your_password_here
- Work on your application
One little word of caution in case you run into the same problem…
After I got all the above done I went to the console (jruby script/
console) and instantiated a table record and tried to save it:
c = Contact.new
c.save!
I got this error:
ActiveRecord::StatementInvalid: ActiveRecord::ActiveRecordError:
IDENTITY_INSERT could not be turned OFF for table…
After I played a little bit with the table definition in the DB using
Microsoft SQL Server Mangement Studio Express I realized that the ID
column, although primary key, was not set as IDENTITY column. I
changed that and… nothing! Same error.
Just for the fun of it I explicitly declared the primary key in my
model:
class Contact < ActiveRecord::Base
self.primary_key = 'ID'
end
That did the trick. It seems you have to specify the primary key or
you won’t be able to save the records.
And one last thing. My table has column names such as FirstName and
LastName. ActiveRecord forced me to use case sensitive symbols during
assignments:
I could use :FirstName => …
I could not use :firstname or :firstName
This last one might have to do with the DB or table definition but I
have not had time to research it to see if making column names case
insensitive is possible under SQL Server. The weird thing is that
using the Management tool I tried to create a column named ‘firstname’
just to check and the tool didn’t let me. ???
Well, I hope this helps somebody else.
Thank you to everybody.