Oct 09, 2020

How to skip undesirable generators in Rails

Rails’s generator commands are awesome, allowing you to scaffold models, controllers, views, and a bunch of other things with a simple command like: However, sometimes the ‘bunch of other things’ like helper files or sass stylesheets aren’t needed. For example, assuming we’re using RSpec, running rails g controller articles will create a bunch of files […]

Sep 03, 2020

How to implement a save / favorite functionality in Rails

Let’s say we’re building a jokes application that allows users to share a bunch of jokes. Now, let’s say we have a new requirement to allow users to save a joke that they particularly liked so they can quickly look that up later. Let’s assume our app has the following models: When designing models or […]

Sep 01, 2020

Useful Rails CLI database commands

Here are some useful database-related CLI commands in Rails. Here for my own reference since I seem to forget all the time! The source code for the different commands can be found here. db:migrate VERSION=0 Revert all migrations. db:migrate:reset Drops the database, recreates it and runs all migrations. db:truncate_all Truncate (empties) all tables in database. […]

Aug 30, 2020

Quick overview of Cron jobs

What is Cron? Cron is a Unix program that lets you schedule and run jobs in the background at specific times or intervals. It is typically used to perform maintenance tasks such as backing up databases or generating daily reports. Jobs scheduled by Cron are known as Cron jobs. These Cron jobs are managed in […]

Aug 24, 2020

Quicky look up Ruby documentation from the terminal

Instead of opening up the Ruby Docs in a web page like this, we can use the ri (Ruby Index) command-line tool to quickly lookup the documentation for a Ruby method, class or module. Ruby is a very rich language with lots of built-in methods so being able to quickly look up the docs for […]

Aug 20, 2020

How to quickly re-seed your Rails database

Sometimes, it’s useful to wipe the database of all existing records and rerun all our seeds in db/seeds.rb . The rails db:seed:replant does exactly this. It doesn’t actually drop our database and rerun our migrations. It only clears / truncates the database.

Aug 19, 2020

Stimulus.js overview

What is Stimulus and how is it different to other JS frameworks? Stimulus is a JavaScript framework by Basecamp (the creators of Ruby on Rails) that provides a refreshing and simple alternative to the intense complexity of modern JavaScript approaches. Unlike contemporary frameworks like React or Vue, it’s not concerned with rendering HTML via some […]