Skip to content
chris edited this page Jul 20, 2023 · 30 revisions

👉 click me 👈 for 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

ruby notes / working with js runtimes

from what i understand execjs ruby library is provided by the rails gem, and is able to pick and choose a JS runtime to execute javascript code.

first error
>> require 'execjs'
Traceback (most recent call last):
    12: from /home/capin/.rvm/gems/ruby-2.7.8/bin/ruby_executable_hooks:22:in `<main>'
    11: from /home/capin/.rvm/gems/ruby-2.7.8/bin/ruby_executable_hooks:22:in `eval'
    10: from /home/capin/.rvm/rubies/ruby-2.7.8/bin/irb:23:in `<main>'
    9: from /home/capin/.rvm/rubies/ruby-2.7.8/bin/irb:23:in `load'
    8: from /home/capin/.rvm/rubies/ruby-2.7.8/lib/ruby/gems/2.7.0/gems/irb-1.2.6/exe/irb:11:in `<top (required)>'
    7: from (irb):1
                6: from /home/capin/.rvm/rubies/ruby-2.7.8/lib/ruby/2.7.0/rubygems/core_ext/kernel_require.rb:147:in `require'
                5: from /home/capin/.rvm/rubies/ruby-2.7.8/lib/ruby/2.7.0/rubygems/core_ext/kernel_require.rb:158:in `rescue in require'
                4: from /home/capin/.rvm/rubies/ruby-2.7.8/lib/ruby/2.7.0/rubygems/core_ext/kernel_require.rb:158:in `require'
                3: from /home/capin/.rvm/gems/ruby-2.7.8/gems/execjs-2.8.1/lib/execjs.rb:4:in `<top (required)>'
                2: from /home/capin/.rvm/gems/ruby-2.7.8/gems/execjs-2.8.1/lib/execjs.rb:5:in `<module:ExecJS>'
                1: from /home/capin/.rvm/gems/ruby-2.7.8/gems/execjs-2.8.1/lib/execjs/runtimes.rb:58:in `autodetect'
                ExecJS::RuntimeUnavailable (Could not find a JavaScript runtime. See https://github.com/rails/execjs for a list of available runtimes.)

the above error is thrown because the ruby interepter can not find a default js runtime to load because by default execjs looks for a predefined set of js runtimes, ie. node, jscore, and whatnot

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