【发布时间】:2011-12-15 00:32:10
【问题描述】:
使用 MYSQL 的 order by field 创建一个 activerecord 查询并调用具有订单的 last 会引发 StatementInvalid 异常。
例如:
ruby-1.9.2-p180 > User.order('field(name, \'joe\')').last
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, "joe") DESC LIMIT 1' at line 1:
SELECT `users`.* FROM `users` ORDER BY field(name DESC, "joe") DESC LIMIT 1
问题是 activerecord 将 DESC 附加到 name 内部和外部的 field 语句。
有没有更好的方法来使用 activerecord 按特定列值排序,或者解决这个问题?
【问题讨论】:
标签: mysql ruby activerecord sql-order-by