2012-01-03 128 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中,以便能夠對所有內容進行排序。

您尚未列出大部分架構,但您應該查看使用羣組查詢來確定頂級飲酒者。