简体   繁体   English

ruby on rails MySQL服务器版本在'附近使用正确的语法? find_by_sql

[英]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.

相关问题 MySQL服务器版本可在附近使用正确的语法 - MySQL server version for the right syntax to use near MySQL服务器版本可在&#39;&#39;附近使用正确的语法 - MySQL server version for the right syntax to use near '' 错误:您的 SQL 语法有错误; 检查您的 MySQL 服务器版本以获取在附近使用的正确语法 - Error: You have an error in your SQL syntax; check your MySQL server version for the right syntax to use near SQL 语法错误; 查看与您的 MySQL 服务器版本对应的手册,了解在附近使用的正确语法 - error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near SQL语法错误; 检查与您的MySQL服务器版本相对应的手册,以在附近使用正确的语法 - error in SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 错误1064(42000):SQL语法; 检查与您的MySQL服务器版本相对应的手册,以在&#39;)附近使用正确的语法; - ERROR 1064 (42000): SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '); MySQL服务器版本,可在&#39;where no =&#39;count&#39;附近使用正确的语法 - MySQL server version for the right syntax to use near 'where no ='count' 错误的MySQL服务器版本,用于在第1行的&#39;&#39;附近使用正确语法 - error MySQL server version for the right syntax to use near '' at line 1 在第1行的&#39;type = heap&#39;附近使用正确语法的MySQL服务器版本 - MySQL server version for the right syntax to use near 'type=heap' at line 1 MySQL服务器版本,可在&#39;VARCHAR(500)附近使用正确的语法 - MySQL server version for the right syntax to use near 'VARCHAR(500))
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM