Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

213
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!