Advanced Querying - belongs_to

This is a companion discussion topic for the original entry at

My question is not really related to belongs_to, but it does relate with joins. Is it possible do something similar to the last example Joe shown, but join on a different attribute than id and role_id?

I know I can do it with joins("INNER JOIN roles on roles.whatever = persons.whatever"), but can I do it with ActiveRecord’s DSL only? Will I need to create a specific relation for that?

Couple of typos in the description for I think:

  • Edit db/schema.rb to get a sense” (should say ‘view’ or ‘open’, since we’re not actually changing it)
  • “Edit spec/models/location_spec.rb and removing the…”

Is there a reason to call all.joins instead of just joins?

1 Like

Using Linux (Ubuntu 14.04) I had some problems with the exercises, since I don’t typically use Postgres. In particular, I was seeing a fe_sendauth: no password supplied (PG::ConnectionBad) error.

Based on this SO question, it looks like the problem was that host: "localhost" in spec_helper.rb was the problem, leading a default Ubuntu postgres installation to expect a password, so I just commented that line out in spec_helper and that worked. Hope that helps someone else.

1 Like

It doesn’t work after I commented the ’ host: “localhost” 'in the ActiveRecord::Base.establish_connection.

Does anyone know how to fix it ?

I had to do a few things to get the setup working for the tests - I’m doing this on Cloud9, which is an Ubuntu virtual machine, so I assume these steps apply on other Linux systems:

  1. Get Postgresql running. On Cloud9 it comes pre-installed, but you need to start it up with sudo service postgresql start
  2. Create a Postgresql database with an owner and password, using the following commands:
CREATE DATABASE advanced_querying_test owner=name;
  1. update the spec_helper with the below:
  adapter:  "postgresql",
  database: "advanced_querying_test",
  username: "name",
  password: "password",
  encoding: "utf8",
  host: "localhost",
  min_messages: "warning"
  1. In the schema file update the class declaration at the top to specify a version of Rails where you’re allowed to inherit from Migrations:
class CreateSchema < ActiveRecord::Migration[4.2]

With all that done, rake successfully ran the tests.


Why did you write has_many :people
instead of
has_many :peopleS ?

Hey @GuillaumeOcculy because Rails handles inflections properly so we can stick to the grammatically correct way of writing :slight_smile:

Hi folks.
After cloning the repo, I’m running the bin/setup command from the terminal. I get this error: The createdb command is required to perform this exercise., and it exits. Anyone else having trouble setting this repo up? Any ideas on how to continue forward?

Hi @arbonap, created is provided with postgres installations. Have you installed postgres? This page has instructions for various OSes (although if you’re on macOS, I’d recommend using homebrew; brew install postgres).

Hi @christoomey. Thanks for the help. I actually have postgres installed via the separate OSX app, not via homebrew. Is the setup shell script written for users who have brew installed postgres?

The setup script doesn’t specifically required brew installed postgres, it only requires that createdb be an available command on your path. It looks like you can enable this in with:

Configure your $PATH to use the included command line tools (optional):

sudo mkdir -p /etc/paths.d &&
echo /Applications/ | sudo tee /etc/paths.d/postgresapp

I am using an M1 Mac, and I couldn’t install ruby
I found a solution with docker that gives
If you are struggling to install either ruby and/or Postgres but can get docker installed, this solution should also work

NOTE This is a replacement for running bin/setup

  1. create a docker-compose.yml file
    build: .
      - .:/app
      - postgres
    command: bin/rake
    image: postgres:14
      - POSTGRES_DB=upcase_exercise
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=password
  1. create Dockerfile
FROM ruby:3.1.2


COPY . /app

RUN bundle install
  1. update spec_helper.rb by replacing the original ActiveRecord::Base.establish_connection with the following
  host: "postgres",
  port: 5432,
  password: "password",
  user: "postgres",
  adapter:  "postgresql",
  database: "upcase_exercise",
  encoding: "utf8",
  min_messages: "warning"
  1. run docker compose build to build the app. This installs and downloads ruby and postgres into containers
  2. run docker compose up -d postgres

To run the tests use docker compose up upcase