我想在 rails app 中执行这个查询

   Video.where("category= 'film' AND grant = 1").order('created_at DESC').page(params[:page]).per_page(10)

这里 grant 存储一个整数值。

我收到以下错误
    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 'grant = 1) ORDER BY created_at DESC LIMIT 10 OFFSET 0' at line 1: SELECT  `videos`.* FROM `videos`  WHERE (category= 'film' AND grant = 1) ORDER BY created_at DESC LIMIT 10 OFFSET 0

提前致谢

最佳答案

问题出在拨款上。 GRANT 是 MySql 中的保留字。你应该在它们周围使用反引号。

Video.where("category= 'film' AND `grant` = 1").order('created_at DESC').page(params[:page]).per_page(10)

会做我猜的工作。引用 Reserved Words

关于ruby-on-rails - 查询以检查 ruby​​ on rails 中的整数值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17718868/

10-11 04:59