Skip to content
Chris Jones edited this page Nov 12, 2020 · 30 revisions

👉 click me 👈 for a good time chrisrjones_rails related notes.

Contents

Ruby Notes

Preamble > Text Editor

A big regret I have is not properly setting my text editor to use spaces for the various rails projects I worked on (╯°□°)╯︵ ┻━┻ ...that said, remember to set your text editor to use 2 two spaces instead of tabs, others will appreciate ❤️

Ruby Style Guidelines

According to the Rubocop README single quotes are preferred over double quotes unless string interoplation is required.

Syntax Checking Ruby and erb files

To lint a ruby file for syntax errors, ie. kind of known as linting a ruby file

ruby -c /path/to/mr-fancy-42-file.rb

To syntax check an embedded Ruby file, ie. an erb file, commonly used with the rails templating engine

  1. Install rails-erb-check
gem install rails-erb-check

To syntax check an erb mr-fancy-42-template-file.html.erb

rails-erb-check mr-fancy-42-template-file.html.erb

Working with RubyGems

To install a specific gem for a specific bundle, ie. insert a gem into the local Gemfile with the version specified

bundle add [gem]

open the the Gemfile and move to appropriate group if needed

A gem is an app for interfacing with ruby gems, ie. packaged ruby apps from RubyGems

To show the RubyGems version

gem -v

To update the installed gems on a system

gem update --system

To list locally installed gems on the system

gem list

Yet another way to show a list of locally installed gems

gem query --local

To install a gem

gem install rails

To check a Gemfile for syntax errors as stated above

ruby -c Gemfile

To read documenation for a particular gem, ie. rails or sinatra

ri rails

To view documentation for a particular version of an installed gem on the sytem in a web browser

gem server

💯 USEFUL To print a list of all available versions of a ruby gem ie. downloadable version from rubygems.org

gem list -ra [GEM_NAME]

Working with PostgreSQL

Installation

To install postgres suite of tools on Debian

apt-get install postgresql-client postgresql postgresql-contrib
sudo -u postgres psql
ALTER USER postgres PASSWORD 'mrFancyPantsPassword';

Then proceed to create a user role who can create, edit, and drop databases in psql.

CREATE USER [mrFancyPantsUser] WITH PASSWORD 'mrFancyPantsPassword';
ALTER USER [mr_fancy_pants_user] CREATEDB;

Definitions

  • foreign key a table column whose values reference rows in another table
  • index a data structure on a table to increase lookup speed
  • schema the structure definition of a database, i.e. tables columns and indexes of DB

psql

To create a role within the database

CREATE USER [mrFancyPantsUser] WITH PASSWORD 'mrFancyPantsPassword';

To list all the postgres roles on the system

\du

To create a database

create database [mr_fancy_pants_database];

To allow an existing postgres user be able to create databases

ALTER USER [mr_fancy_pants_user] CREATEDB;

To allow an existing postgres user be able to drop databases

ALTER USER [mr_fancy_pants_user] SUPERUSER;

To create a Postgres database

createdb <database_name>

To create a Postgres database using the psql monitor

create database mr_fancy_pants;

To create a new DB user for postgres

sudo -u postgres createuser <user_name>
createuser --interactive

To set a database password for a user in Postgres database

su - postgres
psql
alter user <user_name> with password ‘my_secret_password’;

🚨 The preferred way to change a password for a database user

\password

To create a database w/ a user having all privileges on the DB.

su - postgres
psql
grant all privileges on database <database_name> to <userName>;
\q

To dump a postgres database to a .sql file

pg_dump <dbname> > <fileName.sql>

Need to be logged in as postgres user to make this happen.

To import a .sql file into a postgres database, see 🙈

To create a postgres DB user

sudo -u postgres createuser <userName>

Postgres troubleshooting

  • troubleshoot lock file "postmaster.pid" already exists search
    • TL;DR kill the process postmaster.pid process listed in the /usr/local/var/postgres/postmaster.pid file.

If psql complains about libreadline on macOS, more than likely postgres was built against a different version of readline that is currently installed. A typical error message looks like

dyld: Library not loaded: /usr/local/opt/readline/lib/libreadline.6.dylib
Referenced from: /usr/local/bin/psql
Reason: image not found
Abort trap

To get around the above mentioned error message symlink the newer version of libreadline to the previous version of libreadline

ln -sf ./libreadline.8.0.dylib ./libreadline.7.dylib

psql: FATAL: Peer authentication failed for user see 🙈

MySQL Notes

To show databases

mysql> show databases;

To create a database

mysql> create database <db_name>;

To delete a database

mysql> drop database <db_name>;

To create a username for the newly created DB

mysql> grant all privileges on <db_name>.* to ‘username’@ ‘localhost’ identified by ‘password’;

To show permissions for a MySQL user

mysql> show grants for ‘username’@ ‘localhost’;

To show the structure of a table

mysql> show fields from users;

MySQL socket location on a Debian system

/var/run/mysqld/mysqld.sock

Useful Links

Documentation

marketplace platforms

Deploy

Clone this wiki locally