-
Notifications
You must be signed in to change notification settings - Fork 0
Home
👉 click me 👈 for a good time chrisrjones_rails related notes.
- Ruby Notes
- Preamble > Text Editor
- Syntax checking a Ruby file
.rb
- Working with RubyGems
- Rails Notes
- Working with Postgresql
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 ❤️
According to the Rubocop README single quotes are preferred over double quotes unless string interoplation is required.
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 an embedded Ruby file, ie. an erb file, commonly used with the rails templating engine
- 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
A gem is an app for interfacing with ruby gems, ie. packaged ruby apps from RubyGems
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 update gems
gem update --system
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
To print a list of all available versions of a ruby gem ie. downloadable version from rubygems.org
gem list [GEM_NAME] -ra
- rails defaults to the system encoding which is UTF-8 the majority of the time.
- Generally it's a good idea to check and make sure databases have been created before trying to deploy a rails app.
rake db:create:all
To create a rails project without a test suite
rails new [project_name] -T
To delete a model
rails destroy model [model_name]
To run all the migrations for the DB
rake db:migrate
To show the status of the migrations, ie. whether they have been applied or not
rake db:migrate:status
To rollback one migration
rake db:rollback
To clear the contents of the DB
rake db:reset
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;
- 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
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>
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 🙈
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