3

我想在 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

提前致谢

4

3 回答 3

5

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

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

我猜会做的工作。参考保留字

于 2013-07-18T17:04:13.403 回答
4

尝试这个 ..

@videos=Video.where(:category => 'film',:grant => 'yes' ).order('created_at DESC').page(params[:page]).per_page(10)
于 2013-07-18T16:16:20.597 回答
0

检查这个:

Video.where("category = ? AND `grant` = ?", "film", 1).order('created_at DESC').page(params[:page]).per_page(10)
于 2013-07-18T09:07:30.110 回答