Rails new app workflow
These are the steps I take when creating a new Rails app, documented here for when I forget.
Create app
rails new <app_name>
cd <app_name>
git
git init
git add .
git commit –m ‘Initial commit’
Remember: Rails generates a .gitignore file for us.
rvm and gemsets
rvm gemset create <app_name>
echo ‘rvm use 1.9.2@<app_name>’ >> .rvmrc
echo ‘.rvmrc’ >> .gitignore
Creates a .rvmrc file in current directory that way when you cd into this directory rvm automatically switches to the correct version of ruby and the gemset we just created.
Change in and out of the directory to get rvm to kick in:
cd ..
cd <app_name>
Gemfile
Edit ./Gemfile and add the following lines.
gem ‘haml’
group :test do
gem ‘rspec’
gem ‘rspec-rails’
end
Then install and run bundler:
gem install bundler
bundle install
rspec
Generate the rspec files:
rails g rspec:install
Connecting Ruby on Rails to Microsoft SQL Server
NOTE: These instructions are now seriously out of date. You may want to check out my always up to date ebook instead. The secret of connecting Rails to Microsoft SQL Server
It appears rails does not connect to MS SQL Server right out of the box. You need to do the following to get the connection to work:
Get the latest source distribution of Ruby-DBI and copy the file:
bdi-0.1.0/lib/dbd/ADO.rb
to:
c:/ruby/lib/ruby/site_ruby/1.8/DBD/ADO/ADO.rb
NOTE: The ADO directory does not exist on a standard install, you will need to create it.
Then simply set up your railsapp/config/database.yml
Here’s an example for reference:
development: adapter: sqlserver database: database_name host: server_name username: user_name password: your_pw_here
Migrations 'Zombie State' and MS SQL
Getting ‘Zombie State’ errors with migrations and MS SQL Server? Add the following to your config/enviroment.rb:
ActiveRecord::Base.connection.instance_variable_get("@connection")["AutoCommit"] = false
Pluralize table names – no thanks!
Sorry I just can’t live with plural table names, I just can’t get my head around it! Luckily it is easily turned off. In RadRails there is a simple tick box when creating the project which simply adds the following line to the end of your applications config/environment.rb file:
# Include your application configuration below ActiveRecord::Base.pluralize_table_names = false
I may live to regret not embracing plural table names but I doubt it!
Setting up Ruby on Rails
I’m going to give Ruby on Rails (RoR) a try on my Windows XP machine. Here’s how I’ve set it up…
- Install Ruby
Using the Ruby One Click Installer Ruby One Click Installer (v184-16 release candidate 1) - Setup proxy
set HTTP_PROXY=http://wwwcache:8080
- GEM install Ruby on Rails
gem install rails --include-dependencies
- Install RadRails
I also downloaded and installed the latest RadRails (v0.6.1) - Database
I’m going to try rails with our dev MS-SQL server so no database to install. No doubt lots of configuring to do though!
NOTE:
There appears to be a problem with the WEBrick web server in this setup. I couldn’t find the answer on google but I did find a work around. If you run the WEBrick server using the standard ruby script\server you will get the following error:
ruby script\server => Booting WEBrick... => Rails application started on http://0.0.0.0:3000 => Ctrl-C to shutdown server; call with --help for options [2006-04-04 13:20:35] INFO WEBrick 1.3.1 [2006-04-04 13:20:35] INFO ruby 1.8.4 (2005-12-24) [i386-mswin32] [2006-04-04 13:20:35] WARN TCPServer Error: Bad file descriptor - bind(2) c:/ruby/lib/ruby/1.8/webrick/utils.rb:73:in `initialize': Bad file descriptor - bind(2) (Errno::EBADF) from c:/ruby/lib/ruby/1.8/webrick/utils.rb:73:in `create_listeners' from c:/ruby/lib/ruby/1.8/webrick/utils.rb:70:in `create_listeners' from c:/ruby/lib/ruby/1.8/webrick/server.rb:75:in `listen' from c:/ruby/lib/ruby/1.8/webrick/server.rb:63:in `initialize' from c:/ruby/lib/ruby/1.8/webrick/httpserver.rb:24:in `initialize' from c:/ruby/lib/ruby/gems/1.8/gems/rails-1.1.0/lib/webrick_server.rb:59:in `dispatch' from c:/ruby/lib/ruby/gems/1.8/gems/rails-1.1.0/lib/commands/servers/webrick.rb:59 from c:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:21:in `require' from c:/ruby/lib/ruby/gems/1.8/gems/activesupport-1.3.0/lib/active_support/dependencies.rb:136:in `require' from c:/ruby/lib/ruby/gems/1.8/gems/rails-1.1.0/lib/commands/server.rb:30 from c:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:21:in `require' from c:/ruby/lib/ruby/gems/1.8/gems/activesupport-1.3.0/lib/active_support/dependencies.rb:136:in `require' from script/server:3
There appears to be some problem binding to the port 3000, netstat shows nothing running on port 3000 so I’ve no idea what is causing it. Any hoose a work around is simply to run on another port:
ruby script\server --port=8000
If I do find out what is causing the problem I’ll update this post.