1
可能重複:
How would I grab only articles with comments that were created 20 minutes ago?我將如何抓取所有文章並按評論數量排序?
使用的MongoDB和mongoid。如何通過評論的數量獲取所有文章和訂單?
class Article
include Mongoid::Document
has_many :comments
end
class Comment
include Mongoid::Document
belongs_to :article
end
添加計數器緩存(也許[這一個](https://github.com/jah2488/mongoid-magic-counter-cache)),並可以很容易對自己? –
我想過使用計數器緩存,但只是好奇,看看是否有另一種方式,通過計數意見 –
不,沒有其他好方法。如果您希望效率更高,則必須使用計數器緩存。請確保在計數器緩存字段中添加索引。 –