ruby Jun 2, 2020

Common Conventions

Favourite Gems

Simple Form



SearchKick is used to leverage ElasticSearch.

Common conventions

Reindex the entire site from the command line

rails searchkick:reindex:all




  1. Anchor.com
  2. Ruby Pigeon


Factory Bot

FactoryBot is a fixtures replacement with a straightforward definition syntax, support for multiple build strategies (saved instances, unsaved instances, attribute hashes, and stubbed objects), and support for multiple factories for the same class (user, admin_user, and so on), including factory inheritance.



Orderly is a great utility to help test when you need to test the order of content on a page


Faker is a great way to create fake data for testing purposes

Creating unique results for faker data




Figaro is a simple environment management tool that works really well with Heroku.

Deployment steps

figaro heroku:set -e [environment name] --remote [remote name]

Rails Generators

Reference: cdesch cheatsheet on GitHub


rails g scaffold [model name in singular] field_name:field_type

Common field types are

:string, :text, :integer, :float, :decimal, :datetime, :timestamp, :time, :date, :binary, :boolean, :references

Special examples

prices:decimal{8.2} can be used for prices


Reference: Ruby Docs

Foreign References

rails generate migration AddAddressRefToContacts address:references


Reference: Ruby Docs

Creating a date

variable_name = Date.parse('December 23, 2019')

Returning values

variable_name.strftime('%B') returns 'December'

See RubyDocs for more examples



Generating References

You can generate a reference through a rails generator

rails g migration AddPostToUsers post:references

By default, Rails 5 will make this relationship a required field by the model. However, you can override it at the model level

class User < ApplicationRecord

class Post < ApplicationRecord
  belongs_to :user, required: true

Reference: thejspr

You may want to create a reference that has a different name from the model name. To do that, you will need to tweak the migration file

class AddCreatorToOrders < ActiveRecord::Migration[5.2]
  def change
    add_reference :orders, :creator, foreign_key: { to_table: :users }

You will then need to modify the models to reflect the relationship

belongs_to :creator, class_name: 'User'


Shallow Routes

Routing hierarchy quickly gets complicated. One of the strategies suggested by the Rails Guide is to use shallow routing where you split your routes into logical and reasonable structures

Dynamically calling variables and their methods

You can dynamically call methods in Ruby and this is super helpful for testing. Let’s say you have a User class with name and email as it’s methods. You can call it dynamically in your code as the following:

user = User.new
user.email = '[email protected]'
eval("user").send("email") = '[email protected]'

Common Display Conventions

Adding commas to large numbers

<%= number_with_precision(@number, precision: 2, delimiter: ',') %>

Changing cases for string


Changes all letters to be lower case


Changes all letters to be upper case


Changes the first letter of every word to be upper case


Changes the first letter of every sentence to be upper case

Tips and Tricks

Running a rails server that is accessible from another computer

rails s --bind

This binds your rails server to your network card rather than just the default localhost

Convert Strings to Symbols

Symbols are not variables and are closer to variables. The quick way to convert a string to a symbol is to do the following. Let's say you have a symbol


To represent this as a symbol through a string, you could use this:


Active Storage

Checking for file types

@model.content_type will return you the content type.

Checking for image dimensions
Assuming that your content_type is an image, using @model.metadata will return you the width and height of the image in a hash

Decomposing the route that the page came from

 @prev = Rails.application.routes.recognize_path(request.referrer)


Great! You've successfully subscribed.
Great! Next, complete checkout for full access.
Welcome back! You've successfully signed in.
Success! Your account is fully activated, you now have access to all content.