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

218
Vistas
syntax error when using greater than less than in rails query
User.where(is_individual: true)
  .includes(:visitor)
  .where(visitors: { finished: false })
  .where(visitors: { step > 2 })

I am trying to get the out of the above query but it is returning an error

syntax error, unexpected '}', expecting =>)
...visitors: { step > 2 })

But if I do as below then it works.

User.where(is_individual: true)
  .includes(:visitor)
  .where(visitors: { finished: false })
  .where(visitors: { step: 2 })

So the query doesn't work with greater than operator.

I even tried below query and it still doesn't work

User.where(is_individual: true)
  .includes(:visitor)
  .where(visitors: { finished: false })
  .where(visitors: { "step > ?", 2 })
over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

The syntax error you're running into is due to the structure of the argument being passed to the last where clause - { "step > ?", 2 } is not a valid Ruby object.

If using Ruby 2.6 or greater, you can leverage its endless range syntax to construct the query:

User
  .includes(:visitor)
  .where(is_individual: true, visitors: { finished: false, step: 3.. })

If using Ruby 2.5 or below you can produce the same query using Float::INFINITY:

User
  .includes(:visitor)
  .where(is_individual: true, visitors: { finished: false, step: 3..Float::INFINITY })

Alternatively, you can pass a SQL string to where to query on the included model (you'll need to reference the associated table via references):

User
  .includes(:visitor)
  .references(:visitors)
  .where(is_individual: true)
  .where(visitors: { finished: false })
  .where("visitors.step > ?", 2)

You can also explicitly use joins in this case to retrieve the desired results:

User
  .joins(:visitor)
  .where(is_individual: true)
  .where(visitors: { finished: false })
  .where("visitors.step > ?", 2)
over 4 years ago · Santiago Trujillo Denunciar
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