Ruby Mongoid中的按查询排序无法排序
尝试创建一个好的有序列表,我有一个方法,我知道它在参与者类中有效Ruby Mongoid中的按查询排序无法排序,ruby,mongodb,mongoid,Ruby,Mongodb,Mongoid,尝试创建一个好的有序列表,我有一个方法,我知道它在参与者类中有效 def countries_ive_drunk had_drinks.map {|drink| drink.beer.country } end def countries_ive_drunk_count countries_ive_drunk.count end 知道这些都是有效的。我创建了一个新的类,它希望获得一个参与者列表,并按国家/地区/地区/地区/地区/地区/地区/地区/地区/地区/地区/
def countries_ive_drunk
had_drinks.map {|drink| drink.beer.country }
end
def countries_ive_drunk_count
countries_ive_drunk.count
end
知道这些都是有效的。我创建了一个新的类,它希望获得一个参与者列表,并按国家/地区/地区/地区/地区/地区/地区/地区/地区/地区/地区/地区/地区/地区/地区/地区/地区/地区/地区/地区/地区/地区/地区/地区/地区/地区/地区/地区/地区/地区/地区/地区
class Drinkers
include Mongoid::Document
def self.top_ten_drinkers
Participant.order_by([[:countries_ive_drunk_count, :asc]]).limit(10)
end
end
但当这被传递到我的haml文件时:
get '/' do
@topdrinkers = Drinkers.top_ten_drinkers
haml :index
end
那么它的顺序就不正确了。下面的代码生成名称,名称旁边有计数。计数不是asc或desc,只是随机顺序(可能是用户输入mongodb的顺序)
%ul
- @topdrinkers.all.each do |participant|
%li
= "#{participant.countries_ive_drunk_count} - #{participant.name}"
我是否遗漏了任何明显的东西(好一点,这是我第一次使用ruby、mongodb、mongoid、haml……让自己陷入困境!)当您在Mongoid中使用
order\u by
、desc
或asc
时,它会将其传递给MongoDB,以便在数据库服务器上进行排序,并且它会查找一个名为countries\u invoive\u count
的字段,该字段不存在。您需要将数据存储在MongoDB中,以便它能够对所有内容进行排序
您还没有列出很多模式,但是您应该考虑使用组查询来找出最爱喝酒的人