创建一个activerecord查询并使用MYSQL last
调用具有订单的order by field
会抛出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按特定列值进行排序,或者针对此问题采取解决方法?
答案 0 :(得分:0)
有什么问题
User.where("name LIKE ?", \'joe\').last!
如果您只是想要最后一条记录,为什么还要打扰order
?