我使用的Rails 3.2 Ruby和我想知道是否有一種方法可以做如下:是否可以在'order'方法子句中使用問號?
@categories.order('user_id = ? DESC', params[:id])
@categories.order('user_id = ? DESC', @user.id)
# Note: It is almost the same as `@categories.where('user_id = ?', params[:id])`
# but for the `order` clause.
上述聲明生成此錯誤:
ActiveRecord::StatementInvalid (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 '? DESC, ...
您可以使用Ruby字符串插值的ActiveRecord :: Base的。 sanitize_sql_array。 – Hck
我從來沒有使用[sanitize_sql_array](http://apidock.com/rails/ActiveRecord/Sanitization/ClassMethods/sanitize_sql_array)方法。你能提供一個例子說明你如何在我的情況下使用它(使用'order'子句)? – Backo
''@categories.order ActiveRecord :: Base.send(:sanitize_sql_array,['user_id =?',@ user.id])' – Hck