Advanced Querying - belongs_to

This is a companion discussion topic for the original entry at https://thoughtbot.com/upcase/videos/advanced-querying-belongs-to.

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 https://exercises.upcase.com/exercises/advanced-activerecord-querying-belongs_to-associations 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:
psql
CREATE USER name SUPERUSER PASSWORD 'password';
CREATE DATABASE advanced_querying_test owner=name;
  1. update the spec_helper with the below:
ActiveRecord::Base.establish_connection(
  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.

2 Likes

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?
Cheers.

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 Postgres.app with:

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

sudo mkdir -p /etc/paths.d &&
echo /Applications/Postgres.app/Contents/Versions/latest/bin | 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
services:
  upcase:
    build: .
    volumes:
      - .:/app
    depends_on:
      - postgres
    command: bin/rake
  postgres:
    image: postgres:14
    environment:
      - POSTGRES_DB=upcase_exercise
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=password
  1. create Dockerfile
FROM ruby:3.1.2

WORKDIR /app

COPY . /app

RUN bundle install
  1. update spec_helper.rb by replacing the original ActiveRecord::Base.establish_connection with the following
ActiveRecord::Base.establish_connection(
  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