Question

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

Était-ce utile?

La solution

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.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top