Class User
has_many_belongs_to_many :books
end
Class Book
has_many_belongs_to_many :users
end
我正在尝试创建书籍列表并按他们拥有的用户数量对它们进行排序:
Book.find(:all,:conditions => ['title LIKE ?', "%#{params[:autocomplete]}%"],
:limit => 5, :include => :users, :order => "users.count DESC")
不幸的是,这会引发一个错误,告诉我没有用户列:
<pre>Mysql::Error: Unknown column 'users.count' in 'order clause': SELECT `books`.*
FROM `books` WHERE (title LIKE '%pop%') ORDER BY users DESC LIMIT 5</pre>
如果我删除“计数”,我仍然会收到一条错误消息,指出 Mysql 找不到“用户”。知道如何获取这些数据吗?