假设我有一个User带有一个字段的 a name,哪个 has_many teams,一个Team是属于一个的,一个user属于一个的sport。ASport有一个字段name和 has_many teams。
我想遍历,做一些事情,并收集按 排序的sports数组。teamsnameuser
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使用按关系值对集合进行排序的正确方法是什么?