Code & Clay Notes to self. Mainly Ruby/Rails.

Use is_a? when checking class

Do not use == when checking that an object is of a given class.

Whilst it works here:

> Array.new.class == Array
=> true

…it won’t work with descendants of the class we’re checking against:

> class Thing < Array; end
=> nil
> Thing.new.class
=> Thing
> Thing.new.class == Array
=> false

Instead use is_a?:

> Array.new.is_a? Array
=> true
> Thing.new.is_a? Thing
=> true
> Thing.new.is_a? Array
=> true

Enhance shell scripting with Ruby

From Dev_Dungeon:

Ruby is a better Perl and in my opinion is an essential language for system administrators. If you are still writing scripts in Bash, I hope this inspires you to start integrating Ruby in to your shell scripts. I will show you how you can ease in to it and make the transition very smoothly.

I’ve learnt a few things from the article:

Debug output, and anything that does not belong in the output of the application should go to STDERR and only data that should be piped to anothe application or stored should go to STDOUT

There’s ARGF in addition to ARGV.

I can ask the user for a password without the password being echoed back to the terminal:

#!/usr/bin/ruby
require 'io/console'

# The prompt is optional
password = IO::console.getpass "Enter Password: "
puts "Your password was #{password.length} characters long."

I can check for return codes and errors via the $? object.

I can trap interrupt signals with Signal.trap().

#!/usr/bin/ruby

Signal.trap("SIGINT") {
    puts 'Caught a CTRL-C / SIGINT. Shutting down cleanly.'
    exit(true)
}

puts 'Running forever until CTRL-C / SIGINT signal is recieved.'
while true do end

Understanding how to implement a feature that enables users to follow each other

Many sites enable users to follow and be followed by other users. It’s a common feature. Dev.to implements it. Twitter implements it.

Michael Hartl demonstrates an implementation in Chapter 14 of The Ruby on Rails Tutorial. I didn’t really understand what was going on at first. It wasn’t until I’d gained a better understanding of ActiveRecord that I fully comprehended what was going on.

Here, I will explain my understanding of how it works.

Most of my following example is identical to Michael Hartl’s implementation.

What am I trying to implement?

Any user can follow any other user. At the same time, any user can be followed by any other user. The relationship is not reciprocal. That is, if one user follows another user, it is not necessary that the other follows back.

Essentially though, whether or not a user follows or is followed, I am describing two sides of the same relationship – one user points to the other.

  • I will call a user that follows another user a follower.
  • I will call a user that is followed by another user a followee.
  • A user can have many followers. Likewise, a user can follow many followees.
  • I will describe the relationship between a follower and followee as a followship.

Followships

I now have three concepts: followers, followees and followships.

Followships sounds like the name of a table to me. This table will have two columns. One will point to a follower. The other will point to a followee. Each is referenced by its id.

Followships table

Knowing this, I can generate a model named Followship. I will add an id column for followers and followees.

(Followships is a special kind of table called a join table. A join table is used to establish many to many relationships – where models can have many of each other.)

I’ll add an index for both follower_id and followee_id since I will want to look up followships by both followers and followees. Also, I’ll place a unique constraint on the rows because it doesn’t make sense that a user follows another more than once.

class CreateFollowships < ActiveRecord::Migration[5.2]
  def change
    create_table :followships do |t|
      t.integer :follower_id
      t.integer :followee_id

      t.timestamps
    end

    add_index :followships, :follower_id
    add_index :followships, :followee_id
    add_index :followships, [:follower_id, :followee_id], unique: true
  end
end

Before establishing the relationship in the model, there’s a couple of things to be aware of.

Firstly, an association name does not have to share the name of the model it points to. Let’s say I have the classes User and Project. A user can have many projects:

class User < ApplicationRecord
  has_many :projects
end

Each Project belongs to a single User. I could establish the relationship like this:

class Project < ApplicationRecord
  belongs_to :user
end

But I would like to refer to the user as the owner of the project.

So how about this?

class Project < ApplicationRecord
  # This will not work!
  belongs_to :owner
end

Using the above, Rails believes that I want to reference a model name Owner. It infers the class name of the associated object from the name I’ve provided.

So I need to be more explicit.

class Project < ApplicationRecord
  belongs_to :owner, class_name: 'User'
end

Now that I know this technique, I can create belongs_to associations for a followship’s follower and followee. Despite their names, both columns point to User.

class Followship < ApplicationRecord
  belongs_to :follower, class_name: 'User'
  belongs_to :followee, class_name: 'User'

Note: because both of these are belongs_to associations, it is the responsibility of Followship to record the foreign ids of the rows to which it belongs.

From the Rails guides:

4.1 belongs_to Association Reference

The belongs_to association creates a one-to-one match with another model. In database terms, this association says that this class contains the foreign key. If the other class contains the foreign key, then you should use has_one instead.

One final step here. I want to ensure that both the follower_id and followee_id is present. There cannot be a relationship if one is without the other.

The class ends up looking like this:

class Followship < ApplicationRecord
  belongs_to :follower, class_name: 'User'
  belongs_to :followee, class_name: 'User'

  validates :follower_id, presence: true
  validates :followee_id, presence: true
end

Catching breath

So what have I done so far?

  • I have created a model named Followship.
  • Each row in followships points to a follower_id and a followee_id.
  • I’ve indexed by both columns and ensured each pairing is unique.
  • I’ve made sure both ids are present in every row.
  • I’ve established that Followship belongs to a follower and followee. (In doing so, I’ve had to tell Rails that in both cases I’m actually pointing to User.)

Connecting User to the Followship

From the point of view of User, followships come in two flavours.

Depending on which way you look at it, a followship can either be a relationship in which the user is a follower of another or it can be a relationship in which the user is a followee of the other.

In the first case, I want to establish that the user has many relationships in which it follows another user. I want to tell Rails that it should reference the user by follower_id in the followships table.

I want the User model to declare to Rails:

“I can have many followships where I am following another user.

“I will refer to these followships as follower_followships.

“In this case, I will refer to myself as a follower.

“Store my id in followship’s’ follower_id field.”

# app/models/user.rb
# ...
# A follower is a user than follows another user.
has_many :follower_followships,
  class_name: "Followship",
  foreign_key: "follower_id",
  dependent: :destroy

(Like the project/owner example above, I have given the association a name that is different from the class it points to. I also know that because the other class is on the belonging side of the relationship, it is the other class that stores the id. In this case, the user id is stored as the foreign key follower_id.

Notice that I have also made the relationship dependent on the user existing. If the user is destroyed, so too are any rows in followships that correspond to it.)

All good but I haven’t yet pointed to another user. So far, User points only to the join table.

Through

Join tables are called such because they join one table to another. They are the glue in a has_many/has_many relationship. (They provide the belongs_to association that stores the foreign keys). Relationships are made through the join table.

I know that each row has a follower and followee column. I know that if my user follows another, its id will be stored in the follower_id column. I also know that in each row my user is recorded as a follower, there will be the corresponding followee_id.

Now I want the User model to declare:

“I can have many followees.

“I can follow many users. Look up my follower_followships. If you see any, the corresponding followee_id in those rows is the id of the followee I am following.”

Like so:

Follower followships

I declare the has_many association with followees through the previously established follower_followships association:

# app/models/user.rb
# To see the users that the user follows, we reference them through the join
# table.
has_many :followees, through: :follower_followships

So far, the User looks like this:

class User < ApplicationRecord
  # A follower is a user than follows another user.
  has_many :follower_followships,
    class_name: "Followship",
    foreign_key: "follower_id",
    dependent: :destroy

  # We reference the users that the user follows through the join
  # table.
  has_many :followees, through: :follower_followships

Now, I need to do the reverse. I need to establish the other flavour of followship where the user is the followee. A user can be a follower but when other users point to it, it is also a followee.

# A followee is a user that is followed by another user.
has_many :followee_followships,
  class_name: "Followship",
  foreign_key: "followee_id",
  dependent: :destroy

And I can look up followers through the join table:

# To see the users that follow a user, we reference them through the join
# table.
has_many :followers, through: :followee_followships

And now, followees can look up followers through the join table.

Followee followships

A couple of convenience methods

I can now access the users which a user follows by calling self.followees.

Knowing that, I can create two methods that allow me to follow and unfollow users conveniently.

def follow(user)
  followees << user
end

def unfollow(followed_user)
  followees.delete followed_user
end

The final class looks like this:

class User < ApplicationRecord
  # A follower is a user than follows another user.
  has_many :follower_followships,
    class_name: "Followship",
    foreign_key: "follower_id",
    dependent: :destroy

  # To see the users that the user follows, we reference them through the join
  # table.
  has_many :followees, through: :follower_followships

  # A followee is a user that is followed by another user.
  has_many :followee_followships,
    class_name: "Followship",
    foreign_key: "followee_id",
    dependent: :destroy

  # To see the users that follow a user, we reference them through the join
  # table.
  has_many :followers, through: :followee_followships

  def follow(user)
    followees << user
  end

  def unfollow(followed_user)
    followees.delete followed_user
  end
end

In the wild

Dev.to’s implementation is more complicated as it deals with polymorphic associations. However, something similar can be seen in mentor_relationship.rb and in the relevant part of User.rb:

has_many :mentor_relationships_as_mentee,
       class_name: "MentorRelationship", foreign_key: "mentee_id", inverse_of: :mentee
has_many :mentor_relationships_as_mentor,
       class_name: "MentorRelationship", foreign_key: "mentor_id", inverse_of: :mentor
has_many :mentors,
       through: :mentor_relationships_as_mentee,
       source: :mentor
has_many :mentees,
       through: :mentor_relationships_as_mentor,
       source: :mentee

Wrapping up

What have I done?

  • I’ve created a model called Followship.
  • I’ve established the Followship model belongs to a follower and followee.
  • The corresponding table followships is a join table. Users will relate to each other as followers and followees through this table.
  • Each row in the followships table stores a follower_id and a followee_id.
  • I’ve established the a User can have many follower_followships and many followee_followships.
  • A follower_followship (a followship in which the user does the following) is where the user id is stored under followship’s follower_id column.
  • A followee_followhsip (a followship in which the user does is followed) is where the user’s id is stored as a foreign key under followship’s followee_id column.
  • A user can have many followees through follower_followships.
  • A user can have many followers through followee_followships.
  • I’ve added a couple of convenience methods to follow and unfollow users.

What did I need to understand to get here?

  • A model that belongs to another model is responsible for storing the other model’s id as a foreign key.
  • I can establish many to many relationships through a join table.
  • I can give a relation a name that is different from the name of the class it refers to.

Giving an association a custom name

Sometimes, it may be desirable to reference an association with a name that differs from the one generated from the table name.

I have a model named Steps. Each step can have many steps. Each step belongs to one other parent step.

class Step < ApplicationRecord
  belongs_to :step
  has_many :steps
end
a = Step.create
b = Step.create
c = Step.create

a.steps >> b
a.steps >> c

I could get the parent step of b like so:

b.step
# => #<Step:0x00007fdf24b916e8

I don’t think that’s intuitive enough though. Since I’m asking for the parent step, it should be more like:

b.parent
# => #<Step:0x00007fdf24b916e8

Setting up the custom name is simple. I can supply the name of my choosing. Then, all I need to do is specify the class name and foreign id in the options.

class Step < ApplicationRecord
  belongs_to :parent, class_name: 'Step', foreign_key: :step_id
  has_many :steps
end

Converting a pgsql file to CSV

I had a request this morning from a client who wanted me to save some tables from their database as CSVs. They provided a .pgsql file which contains all the data and instructions for constructing the database. It’s sort of a bunch of migrations and seeds in one place.

There might be a more straightforward way but I decided to create a new Rails app and build the database from the dump file. I could then export the tables from the console.

There weren’t many steps to it. Firstly I created the database.

rails db:create

I went to config/database.yml to get the name of the database before running:

psql -d rails_app_development -f dumped_db.pgsql

It took a couple of tries. The command’s output informed me I needed to create some users:

createuser --superuser postgres
createuser --superuser rails

Following rails db:drop db:create I ran the psql command again.

Having created the database I ran db:schema:dump to create the schema file. From that I could see which tables I needed to create corresponding models for.

I then went to the console and dumped my models to CSV files.

def dump_csv(path, model)
  CSV.open(path, "wb") do |csv|
    csv << model.attribute_names
    model.find_each do |row|
      csv << row.attributes.values
    end
  end
end