Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Basic commands for working with a Rails project

Rails commands

irb - command to launch ruby interpreter in any directory
rails c - command lets you interact with your Rails application from the command line
reload! - reload rails environment if you had changed model functionality
gem outdated - show all outdated gems on project
gem update [<gem_name>] - update a specific gem
./bin/webpack-dev-server - start webpacker dev server for fast compiling
code $(bundle show gem_name) - shortcut to open a Ruby gem in VS Code

rails new app_name --webpack=react --database=postgresql -T - create a new app with React, PostgreSQL, without tests
EDITOR=vim rails credentials:edit - edit credential(only inside project folder)
rails routes - show routes in terminal, the same as visit http://localhost:3000/rails/info/routes

rails g controller Articles index show --skip-routes - generate new controller
rails g model User invoice:references person:references amount:integer - generate new modele
rails g serializer User name ip_address device - generate new serializer
rails g channel Activity - generate new chanel

rails db:create db:migrate - create database and run migration
rails db:setup - alias for rails db:reset rails db:migrate rails db:seed
rails db:seed - create some default data in database
rails db:migrate VERSION=20210101 - use specific migration
rails db:rollback STEP=2 - rollback 2 steps back
rails db:migrate:status - show current status of migration

RAILS_ENV=production rake assets:precompile - precompile assets for production environment
RAILS_ENV=production rails s - run server with production environments

debugger - can be used inside model view or controller file to stop standart flow and debug
ActiveRecord::SchemaMigration.where(version: [20210322154930, 20210325113328]).delete_all - delete migrations if these files do'nt currently exist

kill -9 $(cat tmp/pids/server.pid) - kill all running apps with "rails" in the name.

RSpec

rails generate rspec:install - first setup rspec gem
rspec - Run all spec files (i.e., those matching spec/**/*_spec.rb)
rails g rspec:controller users - generate new test for controller
rails g rspec:model user - generate new test for model
rspec spec/models - run all spec files in a single directory (recursively)
rspec spec/requests/invoices_request_spec.rb - run a single spec file
rspec spec/requests/invoices_request_spec.rb:43 - run a single example from a spec file (by line number)
rspec spec -f d - flag that show all test in hierarchy

Running Only Failing Tests

rspec file_name_spec.rb --only-failures rspec person_spec.rb --next-failure - run a single failure at a time

Usefull gem

gem object_tracer - track objects and records their activities, you shold add them in group :development, :test and then call print_traces(object) or print_mutations(object) methods where you wan debug app.

pre-push hook to run tests before push

Rubocop

gem 'rubocop', require: false - add rubocop to Gemfile section group :development, :test
bundle - install new gem
rubocop --auto-gen-config - generate auto config
pre-commit hooks to run rubocop before a commit

Configuration
Update configuration and exclude some of the folders in `.rubocop_todo.yml` file:
AllCops:
  NewCops: enable

Bundler/OrderedGems:
  Exclude:
    - 'db/**/*'
    - 'bin/*'
    - 'config/**/*'
    - 'lib/tasks/*'
    - 'vendor/**/*'
    - 'spec/*'
    - 'node_modules/**/*'

git diff --name-only | xargs rubocop run rubocop for changed files only
rubocop -a or rubocop --auto-correct-all - Auto-correcting offenses
git commit --no-verify - for skip rubocop checking

Work with git

git checkout --track origin/newsletter - Checking Out Remote Branches
git switch -c <new-branch-name> - [c - force it] new way to checkout to the branch
git add . ; git commit -m "fix: short description - add all changes and commit them
git commit --amend - "fix" last commit
git checkout redis_for_heroku -- config/cable.yml - get changes from specific files from another branch
git merge --squash fix_updating_master - squash all changes from a branch to the master
git checkout -b new_branch_name - create new branch and switch to it
git branch -m new_name - rename current branch
git branch -a - show all branches as lists
git branch -D branch_name - delete branch in local scope

git restore foo.js - remove some files that you don't want to commit from staged

git reset --hard origin/master - remove all changes(staged and unstaged)and make it exactly the same as origin/master
git reset --soft origin/master - reset current HEAD to master or squash all current commits to changes
git rebase origin/master - rebase your current branch against master

git stash

git stash -u - add all changes to stash
git stash list - <options> show list of changes
git stash show - <options> <stash> - show changes in the files
git stash pop - get changes from stash and remove it from the stash
git stash apply stash@{n} - unstash your changes without popping them off the stash
git stash push -m "stash_name - create new stash with name

git push -f origin branch_name - force push to the specific branch
git push origin <local_branch>:<remote_branch> - push a local branch to a different remote branch

git cherry-pick COMMIT-HASH-HERE - merge only specific commits from a pull request or branch

git pull --rebase - fetch, merge and then stack your changes on top, all in one shot

git log -1 --pretty=%B - show last commit messages

git reflog - Show history of every action you perform inside of Git where data is stored

git commit --no-verify - aborts the hook and commit changes

git diff ..origin/develop - difference from the origin/master to current branch
git diff origin/develop.. --name-only - all changed/new files in current branch versus to development branch

post-checkout hook to rollback migrations after change the branch

Work with postgres

psql -U postgres - open psql sudo -u postgres psql - login to postgres as postgres user psql [dbname] username - connect to PostgreSQL from the command line
\l - show list of database
\c db_name - connect to the database \d - show list of tables
\d tablename - show datatype of table
select * from tablename; - select all data from a table

DROP TABLE users; - drop table from database
ALTER TABLE people ADD COLUMN salary INTEGER;- add new column to people table
ALTER TABLE people DROP COLUMN salary CASCADE;- remove salary column from people table
insert into schema_migrations (version) values (20210325113328);- insert values into column values
heroku run rails db:migrate:down VERSION=20210001234567 -a app-name
pg_dump kt_development > kt_development.bak - create dump of database
psql test < dbname.bak - restore the database using psql
pg_dumpall > pg_backup.bak - dump all database
psql -f pg_backup.bak postgres - restore all databese

Work in Heroku

heroku login - login to your profile
heroku create special-name-for-the-app - create new heroku app with specific name
heroku buildpacks:add --index 1 heroku/nodejs - because of yarn lib you need use Multiple Buildpacks
git push heroku master - push your project to Heroku server(the main branch may be main)
heroku run rake db:migrate - run migration on Heroku server
heroku pg:reset - drop database on Heroku server
heroku open - open and test your website in a browser
heroku pg:backups:capture - create a new backup
heroku pg:backups:download - download backup
pg_restore --verbose --clean --no-acl --no-owner -h localhost -U myuser -d mydb latest.dump - restore from backup

Work with yarn

yarn add axios - add new package
yarn remove axios - will remove the package

Work with ElasticSearch

brew services start elasticsearch-full - start ElasticSearch
brew services - show all services status
brew services start kibana-full - start Kibana
http://localhost:9200 - show ElasticSearch status
http://localhost:5601 - show Kibana site

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