I always wondered if rails was really working well with SQL Server. A lot of posts on the web concern every database on earth excepted SQL Server. Most posts concerning SQL Server and Rails are written for previous version of rails and deal with specific issues. And sorry, but the official documentation is the jungle ;-)
So I thought it was time for a simple tutorial on how to use SQL Server and rails 2.0 under MS Windows. The goal of this tutorial is to create a basic CRUD application from scratch. The targeted audience is 0 or little knowledge of rails. I'm using SQL Server 2008 CTP6 but this works as well with other versions of SQL Server (2005 or 2000).
After this simple tutorial, I'll try to go more into details in subsequent posts.
First of all, we have to create our application environment :
1. Open a ruby console, go to your rails_apps directory and type
This will create your application file (you should see a lot of files preceded by the create statement).
Then you have to install the SQL Server adapter which was removed from the standard rails 2.0 distribution.
2. In the ruby console, type :
gem install activerecord-sqlserver-adapter --source=http://gems.rubyonrails.org
If you have an error message, try to turn off temporarily the windows firewall (the installation won't take more than a few minutes).
Then you have to create a database on your SQL Server
3. Open your SQL Server client tool and create a database called sqlserverapp
4. Add a user with SQL Server authentication called user with secret as password to the database you just created.
5. Create an ODBC link name sqlserverapp to connect to this database.
Then you have to edit your database configuration file to tell rails to use SQL Server and your new database.
6. In the rails console, go to the configuration directory and edit the database.yml file.
Change the section under development with the following :
Note : replace mycomputer with the name of your instance of SQL Server (in most cases, locahost should do the job).
Repeat the process for the test and production database if needed.
Then the basic configuration is done. We can start writing the application.
7. In the rails console, to generate the scaffold type :
ruby script/generate scaffold Client name:string address:string email:string remark:text
And ... that's almost finished ! This instruction has done all the work, i.e. created the views, prepared your model, your controller, your test files etc...
You just have to run your db migrations and start your server now.
8. In the rails console, type:
This will create your model into SQL Server. If you look into your SQL Server database you should have a table named clients with the columns stated in your scaffold plus two columns for timestamps and one for the primary key which are added automatically.
The last thing you have to do now is to start the web server and test your application.
9. In the rails console, type:
10. Open your web browser and type:
You should now see a form where you can edit your first client. You can also edit/update/delete any client straight away out of the box.
Yes Ruby on rails is magic ! Remember that the whole application took you 2 lines and 10 minutes to write (spending most of the time reading and watching rails work for you).
So now I can say it ! YES, rails does work simply and efficiently with SQL Server 2008 and active records. It's even very simple to use. I no longer will be a frustrated rails and SQL Server user !
Free as in Fortune Cookies - Trello has been out for less than two years and it’s been growing like wildfire. We recently hit 1.5 million members, of whom about 1/3 perform some acti...
3 weeks ago