比方說,我有一個User
與現場name
,和的has_many teams
和Team
是belongs_to的一個user
和belongs_to的一個sport
。 A Sport
有一個字段name
和has_many teams
。如何使用Mongoid對嵌套字段值進行排序?
我想通過sports
行走,做一些東西,並收集由user
的name
排序teams
的數組。
result = []
Sport.asc(:name).each do |spt|
# some other stuff not relevant to this question but that
# justifies my looping through each sport.
spt.teams.asc(:'user.name').each { |t| result << t }
end
這可是運行,並且如預期的那樣sports
的排序,但球隊的result
順序不排序,因爲我所期待。
什麼是正確的方法,使用Mongoid
排序集合的關係值?