My Ruby on rails cheatsheet

Ruby on rails

Instalation

Here's good atricle on how to install Ror: Install ruby on rails

Running ROR

host$ rails new Appname - you can use -d key and specify mysql or postgresql to be used as backend db
host$ cd Appname - change into Appname folder
host$ rails server - or short: rails s

Generating contollers, views etc

host$ cd Appname - change into Appname folder
host$ rails generate (controller|assets|generator|helper|integration_test|jbuilder|job|mailer|migration|model|resource|scaffold|scaffold_controller|task)
Controllername - will generate controller, helper, model etc.

# Generate database migration
host$ cd MyRubyApp1
host$ rails generate migration DoNothingYet
  invoke active_record
  create db/migrate/2015 - 20221103154246_do_nothing_yet.rb

# Generating model
host$ rails generate model User
   invoke active_record
   create db/migrate/2015 - 20221103200833_create_users.rb
   create app/models/user.rb    invoke test_unit
   create test/models/user_test.rb
   create test/fixtures/users.yml

Using rake tool

Rake is script collection to help with ROR application
host$ rake -T - Will show all tasks that rake can do, eg
host$ rake db:schema:dump - will dump db schemas in use
# Executing migrations and chose environment on whitch it will be executed (development is default)

host$ rake db:migrate RAILS_ENV=development
== 2015 - 20221103200833 CreateUsers: migrating ======================================
-- create_table(:users)
-> 0.0022s
== 2015 - 20221103200833 CreateUsers: migrated (0.0024s) =============================

# Rollback migrations to initial state
host$ rake db:migrate VERSION=0 - it is important that version is in capital letters as it's environment variable

# Do forward migrations
host$ rake db:migrate:status - will show which migrations are up (executed) or down (rolled back)
host$ rake db:migrate VERSION=2015 - 202209012015 - 202222 - where version value is output from command above
host$ rake db:migrate:up VERSION=2015 - 202209012015 - 202222 - do this migration
host$ rake db:migrate:down VERSION=2015 - 202209012015 - 202222 - undo this migration
host$ rake db:migrate:redo VERSION=2015 - 202209012015 - 202222 - do first down and then up for this migration
# if you don't provide VERSION rake assumes latest version