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
?
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 :people
instead of
has_many :peopleS
?
Hey @GuillaumeOcculy because Rails handles inflections properly so we can stick to the grammatically correct way of writing
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 install
ed 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
- 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
- create
Dockerfile
FROM ruby:3.1.2
WORKDIR /app
COPY . /app
RUN bundle install
- update
spec_helper.rb
by replacing the originalActiveRecord::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"
)
- run
docker compose build
to build the app. This installs and downloads ruby and postgres into containers - run
docker compose up -d postgres
To run the tests use docker compose up upcase