1
我想通過一個集合中的值來關聯一個集合。如何通過一個mongoid協會訂購
例子:
我multible協會的帖子,如:
- 評論
- 等級
- 附着物
我怎樣才能訂購帖子通過這個協會,如:
- ORDER_BY most_commented
- ORDER_BY most_rated
- ORDER_BY most_associations ....
謝謝。
我想通過一個集合中的值來關聯一個集合。如何通過一個mongoid協會訂購
例子:
我multible協會的帖子,如:
- 評論
- 等級
- 附着物
我怎樣才能訂購帖子通過這個協會,如:
- ORDER_BY most_commented
- ORDER_BY most_rated
- ORDER_BY most_associations ....
謝謝。
現在我可以回答這個問題^^
隨着Mongoid version 3.1活動記錄功能 「counter_cache」 是aviable。 比如我買了參考意見後:
class Post
include Mongoid::Document
field :body, type: String
has_many :comments
end
class Comment
include Mongoid::Document
field :body, type: String
belongs_to :post, counter_cache: true
end
在這種情況下,每一個崗位的實例有一個comments_count場至極包含的評論的數量在後參考。
現在,您可以使用comments_count字段對您的帖子進行排序。 請記住,只有至少有一條評論存在時,此字段纔可用。 或者將模型中的comments_count字段顯式設置爲默認值。
我真的很想知道這一點。 – 2013-05-06 13:36:38