← Back to Upcase

Advanced Querying - belongs_to


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

(Luís Ferreira) #4

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?


(Andy Waite) #5

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…”

(Jacob Chae) #6

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


(Peter Northup) #7

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.


(cosnaye) #8

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

Does anyone know how to fix it ?


(Ollie Haydon-Mulligan) #9

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.


(GuillaumeOcculy) #10

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


(Zaid Al Lahham) #11

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


(Patricia Arbona) #12

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.


(Chris Toomey) #13

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).


(Patricia Arbona) #14

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?


(Chris Toomey) #15

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