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
rails new __<project name>__ -d postgresql -T
- Generates a new rails project folder in the current folder
- -d : Database
- -T : Skip test unit
cd __<project folder>__
- Goes into newly created project folder
bin/rake db:create
- Instantiates a new database
- Ignoring this step will cause a NoDataBase exception later
- (optional) Add gems to Gemfile and run
bundle install
- This will ensure any additional gems are added
bin/rails g controller __<controller name>__
- CONTROLLER NAMES ARE PLURAL (e.g.
questions
, notquestion
)
- Edit the
routes.db
file.
- Found at
<project folder>/config/routes.db
- This is where path info is set for each web page
bin/rails g model __<model name>__
- MODEL NAMES ARE SINGULAR (e.g.
question
notquestions
)
- 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
bin/rake db:migrate
andbin/rake db:rollback
Migrate
will move our fields/data to the database that was instantiated earlierRollback
will remove fields/data to the database that was instantiated earlier
- Go to
<project folder>/db/seeds.rb
- 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
bin/rake db:seed
- (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
Courtesty to @malberton for refactoring this gist 🙂