[英]ruby on rails MySQL server version for the right syntax to use near '? find_by_sql
view: 视图:
<% Favorite.find_by_sql("SELECT p.* FROM favorites as f LEFT JOIN posts as p ON p.user_id = f.user_id WHERE f.user_id = ? limit 15", @user.id).reverse.each do |post| %>
i am getting the following 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 '? limit 15' at line 1: SELECT p.* FROM favorites as f LEFT JOIN posts as p ON p.user_id = f.user_id WHERE f.user_id = ? limit 15
what i am basically trying is to get posts the i have favorited 我基本上想做的是获取我喜欢的帖子
Try 尝试
<% Favorite.find_by_sql(["SELECT p.* FROM favorites as f LEFT JOIN posts as p ON p.user_id = f.user_id WHERE f.user_id = ? limit 15", @user.id]).reverse.each do |post| %>
Need to send the query and it's parameters as an array to get the argument replace occurring. 需要将查询及其参数作为数组发送,以使参数替换发生。
Are you aware this method is deprecated, not db agnostic so an absolute last resort and a maintenance nightmare. 您是否知道此方法已过时,而不是与数据库无关,所以绝对是不得已而为之,也是维护的噩梦。
Should be a method on your model to do this, putting sql in a view is not just going off the rails it's a catastrophic train wreck. 应该是模型上执行此操作的一种方法,将sql放在视图中不仅会偏离轨道,而且还会造成灾难性的火车事故。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.