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
rails sqlserverapp
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.
cd configuration
edit database.yml
Change the section under development with the following :
development:
adapter: sqlserver
mode: odbc
dsn: sqlserverapp
username: user
password: secret
host: mycomputer
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:
rake db:migrate
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:
ruby script/server
10. Open your web browser and type:
http://localhost:3000/clients
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

10 comments:
please help me
when do the step:rake db:migrate
no such file to load -- dbi
rake db:migrate
:
** Invoke db:migrate (first_time)
** Invoke environment (first_time)
** Execute environment
** Execute db:migrate
rake aborted!
no such file to load -- dbi
/usr/local/lib/site_ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require'
/usr/local/lib/site_ruby/1.8/rubygems/custom_require.rb:31:in `require'
/usr/lib/ruby/gems/1.8/gems/activesupport-2.1.2/lib/active_support/dependencies.rb:510:in `require'
.....
why? Thank U very much, and you have done the good blog, i have learned much more about rails.
Did you install the SQL-Server adapter ? Did you set-up the odbc ?
thanks for the tip. i was able to make it work. however, what do you need to do to make it work using the ADO. It seems all the tutorial on using ADO does not work.
I am newbie to SQL and Rails
I have followed your in instructions but I am getting this error:
>ruby script/generate scaffold Client name:string address:string email:string remark:text
c:/ruby/lib/ruby/1.8/yaml.rb:133:in `load': syntax error on line 3, col 5: ` dsn: sqlserverapp' (ArgumentError)
from c:/ruby/lib/ruby/1.8/yaml.rb:133:in `load'
from c:/ruby/lib/ruby/gems/1.8/gems/rails-2.2.2/lib/initializer.rb:819:in `database_configuration'
from c:/ruby/lib/ruby/gems/1.8/gems/rails-2.2.2/lib/initializer.rb:391:in `initialize_database'
from c:/ruby/lib/ruby/gems/1.8/gems/rails-2.2.2/lib/initializer.rb:139:in `process'
from c:/ruby/lib/ruby/gems/1.8/gems/rails-2.2.2/lib/initializer.rb:112:in `send'
from c:/ruby/lib/ruby/gems/1.8/gems/rails-2.2.2/lib/initializer.rb:112:in `run'
from C:/Users/ddamko/Documents/Aptana Studio/sqlserverapp/config/environment.rb:13
from c:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require'
from c:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `require'
from c:/ruby/lib/ruby/gems/1.8/gems/rails-2.2.2/lib/commands/generate.rb:1
from c:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require'
from c:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `require'
from script/generate:3
Hey Vincent, I am not sure what you mean by "Create an ODBC link name sqlserverapp to connect to this database". I only know the basics about MS Sql Server.
Thanks!
I get a similar problem to cona,
rake db:migrate
(in C:/dev/sqlserverapp)
** Invoke db:migrate (first_time)
** Invoke environment (first_time)
** Execute environment
** Execute db:migrate
rake aborted!
no such file to load -- deprecated
C:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require'
C:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `require'
There is lots more error info
I have got database set up and tested odbc connector and installed sql server adapter. In fact until all this worked I did not get a file to migrate.
(ruby 1.8.6)(rails 2.3.2)(active-record-sqlserver-adapter 1.0.0.9250)
As an alternative...
1. install activerecord sqlserver gem as above
2. create the database on your server
3. use the following for the database.yml
development:
adapter: sqlserver
mode: odbc
dsn: Driver={SQL Native Client};Server=.\SQLEXPRESS;Database=rails_database_name;
Where SQLEXPRESS is replaced with your SQL Server instance, and rails_database_name is the name of the database you creasted
Note: this assumes you are using Integrated Security for your sql server, you will need to addjust the connection string (dsn) accordingly for your setup.
This works under a normal windows rails install, as well as a cygwin installation
Is it possible to connect to a sql server in another machine?
I wrote a tutorial which explains how to use RoR with SQL Server 2008. The tutorial is geared for .NET developers: A Ruby on Rails Tutorial for .NET Developers
Thank you for the clear documentation. After visiting a lot of websites without result your post did the job immediately. THANKS!!!!
Post a Comment