2012-01-03 130 views
0

力图打造一个漂亮的有序列表,我有我知道的工作,是它们在与会者类订单在Mongoid查询失败订购

def countries_ive_drunk 
    had_drinks.map {|drink| drink.beer.country } 
    end 

    def countries_ive_drunk_count 
    countries_ive_drunk.count 
    end 

Knowning这些上述工作的方法。我创建了一个新的类,它想通过countries_ive_drunk_count

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 

然后它不是” t正确排序。下面的代码会导致名称旁边有计数。数目不递增或递减,并且只按随机顺序(可能是为了用户订立的MongoDB

%ul   
    - @topdrinkers.all.each do |participant| 
     %li 
     = "#{participant.countries_ive_drunk_count} - #{participant.name}" 

我缺少什么明显的(是不错的使用我的第一次红宝石,MongoDB的,mongoid, HAML ...投掷自己在深结束!)

回答

1

当您使用order_bydesc或Mongoid asc,它是将它传递给的MongoDB数据库服务器上进行排序,并在寻找一个名为countries_ive_drunk_count场,这没有按不存在,您需要将数据存储在MongoDB中,以便能够对所有内容进行排序。

您尚未列出大部分架构,但您应该查看使用群组查询来确定顶级饮酒者。