Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

219
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda