A basic cheat sheet for a Rails 5 project at Turing School.
Written by Anthony Ongaro, on 2023-01-23
Begin in terminal
rails new application_name -T -d="postgresql" --skip-spring --skip-turbolinks
Command breakdown:
-T
remove standard testing (add RSpec later)-d="postgresql"
use PostgreSQL for database (SQLite3 by default)--skip-spring
& --skip-turbolinks
skip things we don’t need at this scaleAdd gems to :development :test group in Gemfile
group :development, :test do
gem 'pry' # binding.pry
gem 'rspec-rails'
gem 'capybara' # feature tests
gem 'launchy' # save_and_open_page
gem 'faker' # use with factory_bot to generate fake data for tests
gem 'factory_bot_rails'
gem 'simplecov' # requires add'l setup
gem 'shoulda-matchers' # requires add'l setup
gem 'orderly' # tests order of appearance on webpage
end
Bundle Install
Run bundle
to install the gems from Gemfile
Install RSpec:
rails generate rspec:install
In .gitignore:
Add /coverage/
directory to bottom of file to ignore SimpleCov
In rails_helper.rb: Add code to top of file for SimpleCov
require 'simplecov'
SimpleCov.start
Add this code to bottom of file for Shoulda-Matcher (association testing)
Shoulda::Matchers.configure do |config|
config.integrate do |with|
with.test_framework :rspec
with.library :rails
end
end
Create New GitHub Repository, Add Remote Repo As Origin
git remote add origin git@github.com:<user_name>/<repo_name>.git
Create Local Initial Commit, Push To Origin
git push -u origin main
Probably create a new branch and then…
Build the PostgreSQL test and development databases for the project:
rails db:create
Use dbdesigner to design initial schema before next steps. If you already have a schema.rb in place, dbdiagram is amazing. It lets you import directly from a copy and pasted schema.
Generate migrations for each initial table in database with columns and data types Table names are always plural
rails generate migration CreateSongs title:string length:integer play_count:integer
rails generate migration CreateArtists name:string age:integer :integer
( You’ll want to investiate using rails generate model Songs...
later. )
Add t.timestamps
to your new migration files in db
directory (adds created_at/updated_at)
Edit any mistakes you made with your terminal commands here before migrating.
class CreateInstruments < ActiveRecord::Migration[5.2]
def change
create_table :instruments do |t|
t.string :brand
t.string :type
t.boolean :used
t.boolean :needs_repair
t.integer :price
t.timestamps
end
end
end
Once done creating all migration files
rails db:migrate
You can do this before or after the previous step, or any time in the future Create your database relationships to generate foreign_ids for ‘child’ tables
rails generate migration AddArtistToSongs store:references
Note: the singular parent class being added to the plural child class
An Artist
has_many Songs
, this is how you add a foreign id to Songs to identify its artist_id
Make sure everything is correct, and then migrate changes into db.
rails db:migrate
Once you have your initial tables set up, then you can migrate the relationships.
rails g migration CreateSnackMachines snack:references machine:references
This creates snack and machine relationship, with snack id + machine id
Verify schema.rb
is all correct at this point or continue setting up tables and relationships as needed.
Seed database with db/seeds.rb
or go into rails console
and start adding stuff
Start building tests, start model validations and relationships. These are just examples of different relationships tests and validations.
require 'rails_helper'
RSpec.describe User, type: :model, do
describe 'relationships' do
it { should belong_to :movies }
it { should have_many :user_parties }
it { should have_many(:parties).through(:user_parties) }
end
describe 'validations' do
it { should validate_presence_of :name }
it { should validate_presence_of :email }
it { should validate_uniqueness_of :email }
end
end
# Start rails development server:
rails s rails server
# Start rails sql console
rails dbconsole
# Rails console to interact with objects/db:
rails c / `rails console`
# Drop, create, migrate database (!!: This Destroys Development DB)
rails db:{drop,create,migrate,seed}
# Seed from db/seeds.rb
rails db:seed
# Check project's routes in terminal (important)
rails routes
# Build models
rails generate model Task user:references name:string frequency:integer