1

Doing any one of these:

notifications.where("read = 'true'")
notifications.where("read = '1'")
notifications.where("read = true")
notifications.where("read = 1")

results in the error:

Mysql2::Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'read = 'true')

However, this works just fine:

notifications.where(:read => true)

Any idea why this could be?

1 Answer 1

1

read is mysql reserved keyword you need to use back-ticks around your column name

notifications.where(" `read` = true")

Not familiar with ruby but you can refer to this answer to enclose the column with back-ticks

Mysql Reserved Words

Sign up to request clarification or add additional context in comments.

2 Comments

Oh wow ok. Is it safe to use it still or should I user another name?
Best practice is use the meaning full column name like user_read or notification_read so it has the minimal chance to produce such type of errors and you can easily use the column names without back-ticks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.