Active Record is an object-relational mapping (ORM) that makes it easy to work with databases. While there are other ORMs out there for Ruby, Active Record is very popular and I wanted to walk through using it with SQL Azure. Active Record can be installed by running gem install activerecord, however most people probably install it as part of the Rails installation. The examples in this post use Rails, so gem install rails is the command I used to install Rails, Active Record, and all the other bits that Rails comes with.
Next we need an adapter that Active Record can use to talk to SQL Azure. I’m using the SQL Server Adapter (gem install activerecord-sqlserver-adapter,) though I’m told that if you’re using JRuby that the activerecord-jdbc-adapter works as well.
The Active Record SQL Server adapter can connect to SQL Azure using either ODBC (through the Ruby-ODBC gem,) or dblib (through the TinyTDS gem.) While both allow connectivity, I’ll be using ODBC since the TinyTDS gem currently requires a manual build process to enable SQl Azure support. If you’re interested in using TinyTDS, I documented my experience building it with SQL Azure support at http://social.technet.microsoft.com/wiki/contents/articles/connecting-to-sql-azure-from-ruby-applications.aspx#tinytds. To install the Ruby-ODBC gem, use gem install ruby-odbc.
Now we just need to provision a new SQL Azure server, create a database, create a Rails application, and modify it to use our SQL Azure database.
Perform the following steps to provision a new SQL Azure server:
At this point you will have a new server with a gibberish name like fvwerou3.database.windows.net. You can use the Test Connectivity button on the ribbon at the top of your browser to test whether you can connect to the master database for this server.
Active Record can’t automatically create a database on the SQL Azure server that we’ve just provisioned, so we have to do this manually. To create a new database, select the Create button from the Database section of the ribbon.
Enter a new name and click OK. The defaults of Web edition and a size of 1gb should be sufficient for testing.
For a test application, I created a simple blog using the following commands at the command line:
rails new blogcd blograils generate scaffold Post title:string body:text
To configure this application to use SQL Azure, perform the following steps:
gem ‘activerecord-sqlserver-adapter’gem ‘ruby-odbc’
development: adapter: sqlserver mode: ODBC dsn: Driver={SQL Server};Server=servername.database.windows.net;Uid=user@servername.database.windows.net;Pwd=password;Database=databasename
At this point you willll receive an error staging that ‘Tables without a clustered index are not supported in this version of SQL Server’. To fix this error, go back to the Windows Azure portal in your web browser and perform the following steps:
CREATE CLUSTERED INDEX [idx_schema_migrations_version] ON [schema_migrations] ([version])
You can now start the blog web site by running rails s. Navigate to http://localhost:3000/posts to create and view posts, which will be stored into SQL Azure. If you return to the browser window where we added the clustered index, you should be able to select the ‘posts’ table and view the table structure, data stored in it, etc.
In writing this blog post I’ve noticed that something has changed between my the Rail 3.1RC code my work environment is using vs. the Rails 3.1 release code I installed on my test box. The RC4 code works fine with SQL Azure, but with the 3.1 release I receive the following error:
DBCC command ‘useroptions’ is not supported in this version of SQL Server
This is accurate; useroptions isn’t supported by SQL Azure. So if you're using a newer version of Rails and things are failing, you're not alone. I'll investigate and see if I can find a resolution to this error and post an update here.
Hi Larry,
very helpful post - Thank you!
It looks like the issue (Rails 3.1+) has been solved a couple months back (github.com/.../fdb7cb05). Maybe you would like to update your post as it shows up in the top spot if searching for "sql azure with rails"?
Cheers,
Nikolai