Search code examples
ruby-on-railsrubyruby-on-rails-5

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 })

Solution

  • 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)