Ruby on Rails

@Ruby, @Ruby_on_Rails

.. TODO -

Installing Rails


gem install rails

Setting up Rails


rails new app_name
cd app_name
rake db:create
rails server # (on standalone machine)
rails generate controller home index
rm public/index.html
# Uncomment the root :to => "home#index" line
$EDITOR config/routes.rb



rails generate scaffold Post user:references title:string{50} content:text

Add indexes to migration


rails g resource user name:index email:uniq

Using Dreamhost

Validating Active Records

class Post < ActiveRecord::Base
  # ...

  validates :name,  :presence => true
  validates :title, :presence => true,
       				 :length => { :minimum => 5 }

Ensure uniqueness at the db level

# rails generate migration add_index_to_users_email
# add to migration file under def change: add_index :users, :email, unique: true
# bundle exec rake db:migrate


Migrate to new model


rake db:migrate

Return to previous model


rake db:rollback

Automated testing with guard and spork

*Add to Gemfile:

group :test, :development do
  gem 'guard-spork'
  gem 'spork'
  gem 'guard-rspec'
group :test do
  gem 'rb-inotify'
  gem 'libnotify'

bundle exec guard init rspec
bundle exec spork --bootstrap
bundle exec guard init spork

Creating tables

create_table "contacts" do |t|
  t.integer  "user_id", :null => false
  t.string   "name", :null => false
  t.string   "phone", :limit => 40
  t.string   "email"

Reset test database


bundle exec rake db:test:prepare

Find code comment dev tags

To find TODO, FIXME, and OPTIMIZE comment tags, use rake notes

Simple wins

Get versions

rake about