head.daveops.net

Snippets for yer computer needs

Ruby on Rails

Installing and setting up Rails

gem install 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

Scaffolding

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 }
end

Ensure uniqueness at the db level

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

Database

# Migrate to new model
rake db:migrate

# Return to previous model
rake db:rollback

Automated testing with guard and spork

group :test, :development do
  gem 'guard-spork'
  gem 'spork'
  gem 'guard-rspec'
end
group :test do
  gem 'rb-inotify'
  gem 'libnotify'
end
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"
end

Reset test database

bundle exec rake db:test:prepare

Rake default niceties

# Find TODO, FIXME, and OPTIMIZE comment tags
rake notes

# Get versions
rake about

Simple wins