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
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
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.
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:
- Get Postgresql running. On Cloud9 it comes pre-installed, but you need to start it up with
sudo service postgresql start
- 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;
- 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" )
- 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 :peopleS ?
Hey @GuillaumeOcculy because Rails handles inflections properly so we can stick to the grammatically correct way of writing
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?
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:
$PATHto 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
- create a
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
FROM ruby:3.1.2 WORKDIR /app COPY . /app RUN bundle install
spec_helper.rbby replacing the original
ActiveRecord::Base.establish_connectionwith the following
ActiveRecord::Base.establish_connection( host: "postgres", port: 5432, password: "password", user: "postgres", adapter: "postgresql", database: "upcase_exercise", encoding: "utf8", min_messages: "warning" )
docker compose buildto build the app. This installs and downloads ruby and postgres into containers
docker compose up -d postgres
To run the tests use
docker compose up upcase