Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

233
Vistas
Rails 5 left outer join with where() conditions excludes rows from left table

I have a Qualifiers table storing various terms used to qualify movies. Some of them have a kind :tag or :pool and can be :active or not. They are attributed to movies through a Movietags relationship table, relatively to an edition, which changes each year.

class Qualifier < ApplicationRecord
  has_many :movies
  has_many :qualifiers, through: :movietags
  has_many :movietags, inverse_of: :qualifier, dependent: :destroy
  ...
end

class Movietag < ApplicationRecord
  belongs_to :edition
  belongs_to :movie
  belongs_to :qualifier
end

My goal is to get all the active tags (hence the two scopes) along with their movietags of any given edition (and only those). This query works and does a LEFT OUTER JOIN but excludes the qualifiers with movietags not used in the given edition:

@qualifiers = Qualifier.all_tags.active
      .includes(:movietags)
      .where('movietags.edition_id = ? OR movietags.edition_id IS NULL', @edition_id)
      .references(:movietags)

-- irrelevant parts removed for legibility
SELECT "qualifiers"."id" AS t0_r0, "qualifiers"."kind" AS t0_r1, "qualifiers"."active" AS t0_r2, 
"movietags"."id" AS t1_r0, "movietags"."edition_id" AS t1_r1, "movietags"."movie_id" AS t1_r2, 
"movietags"."qualifier_id" AS t1_r3 FROM "qualifiers" LEFT OUTER JOIN "movietags" ON 
"movietags"."qualifier_id" = "qualifiers"."id" WHERE "qualifiers"."kind" IN ('tag', 'pool') AND 
"qualifiers"."active" = $1 AND (movietags.edition_id = 5 OR movietags.edition_id IS NULL) [["active", "t"]]

How can I get all the active tags?

I'm looking for a pure Rails solution, I'm aware that direct SQL queries can achieve this (see comments, thanks @philipxy).

over 4 years ago · Santiago Trujillo
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda