Rails查询布尔MySQL错误

时间:2014-02-16 05:58:38

标签: mysql ruby-on-rails

执行以下任何一项:

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

导致错误:

  

Mysql2 ::错误:您的SQL语法中有错误;检查手册   对应于您的MySQL服务器版本,以获得正确的语法   在'read ='true')附近使用

但是,这很好用:

notifications.where(:read => true)

知道为什么会这样吗?

1 个答案:

答案 0 :(得分:1)

读取是mysql保留关键字,您需要在列名称后面使用反斜杠

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

不熟悉红宝石,但你可以参考this answer用反尾标记来封闭专栏

Mysql Reserved Words