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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top