Skip to content

Instantly share code, notes, and snippets.

@samanthaming
Last active April 7, 2018 21:16
Show Gist options
  • Save samanthaming/2bf5c52df35103a989ff3f7a773e0956 to your computer and use it in GitHub Desktop.
Save samanthaming/2bf5c52df35103a989ff3f7a773e0956 to your computer and use it in GitHub Desktop.
New Rails Project Cheat Sheet

New Rails Project Cheat Sheet

Author : Samantha Ming
Date : February 03, 2016

Note : In Atom, click on 'Packages' --> 'Markdown Preview'.
This allows you to see these notes with markdown formatting ​

New Rails Project

  1. rails new __<project name>__ -d postgresql -T
  • Generates a new rails project folder in the current folder
  • -d : Database
  • -T : Skip test unit
  1. cd __<project folder>__
  • Goes into newly created project folder
  1. bin/rake db:create
  • Instantiates a new database
  • Ignoring this step will cause a NoDataBase exception later
  1. (optional) Add gems to Gemfile and run bundle install
  • This will ensure any additional gems are added
  1. bin/rails g controller __<controller name>__
  • CONTROLLER NAMES ARE PLURAL (e.g. questions, not question)
  1. Edit the routes.db file.
  • Found at <project folder>/config/routes.db
  • This is where path info is set for each web page ​

New Model

  1. bin/rails g model __<model name>__
  • MODEL NAMES ARE SINGULAR (e.g. question not questions)
  1. Edit the migration file
  • Found at <project folder>/db/migrate/<file name>.rb
  • Set up the fields here. e.g.
t.string :title # Sets up the title field in a database to accept a string
t.text :body # Sets up the body field in the database to accept a body of text
t.decimal :number # Sets up the number field to accept a floating point value
  1. bin/rake db:migrate and bin/rake db:rollback
  • Migrate will move our fields/data to the database that was instantiated earlier
  • Rollback will remove fields/data to the database that was instantiated earlier ​

Seeding the Database

  1. Go to <project folder>/db/seeds.rb
  2. Fill with code appropriate for seeding the database with data
  • One option is to use the Faker gem to populate the database with fake information
  • e.g.
100.times do
 Product.create   name: Faker::Commerce.product_name,
                  description: Faker::Company.bs,
                  price: Faker::Commerce.price
end
  • This will fill the name, description, and price fields with fake information
  1. bin/rake db:seed
  2. (optional) bin/rails c, then <model name>.all
  • Do this to access the database and check if the seeding was performed correctly
  • Can also use pgAdmin3 or other methods to check database

Special Thanks 👏

Courtesty to @malberton for refactoring this gist 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment