PG :: UndefinedFunction:ERROR LIKE运算符

时间:2016-08-27 08:44:52

标签: ruby-on-rails postgresql

在我的Rails项目中,当我试图进行评分时,我得到了这个错误。

错误:

  

PG :: UndefinedFunction:ERROR:运算符不存在:整数~~   整数LINE 1:... dbacks"在哪里"反馈"。" user_id" = $ 1 AND   (等级5)^提示:没有运算符匹配给定的名称和   参数类型。您可能需要添加显式类型转换。 : 选择   COUNT(*)FROM"反馈"在哪里"反馈"。" user_id" = $ 1 AND(评级   像5)

控制器代码:

unless @user.feedbacks.count == 0
  @rating = ((@user.feedbacks.where("rating LIKE 5").count*5 + @user.feedbacks.where("rating LIKE 4").count*4 +
  @user.feedbacks.where("rating LIKE 3").count*3 +
  @user.feedbacks.where("rating LIKE 2").count*2
 +@user.feedbacks.where("rating LIKE 1").count).to_f / @user.feedbacks.count).round(2)
else
  @rating = 0
end

有人能帮助我吗?

1 个答案:

答案 0 :(得分:0)

The PostgreSQL documentation

  

如果字符串与提供的匹配,则LIKE表达式返回true   图案。 (正如预期的那样,如果LIKE,NOT LIKE表达式返回false   返回true,反之亦然。等价表达式是NOT(字符串   喜欢模式)。)

您不能将LIKE运算符与整数一起使用。编辑您的查询。 像这样:

"rating = 5"