2013-05-31 62 views
0

我有四種類型的Rawscores,我想在分頁表中一起顯示。使用來自多個陣列的屬性對模型進行分頁

代碼如下:

@facebook_scores_today = Rawscore.where(:channel_string => "Facebook", :project => @project, :created_at.gt => Time.now.beginning_of_day) 

@youtube_scores_today = Rawscore.where(:channel_string => "Youtube", :project => @project, :created_at.gt => Time.now.beginning_of_day) 

@twitter_scores_today = Rawscore.where(:channel_string => "Twitter", :project => @project, :created_at.gt => Time.now.beginning_of_day) 

@influence_scores_today = Rawscore.where(:channel_string => "Influence", :project => @project, :created_at.gt => Time.now.beginning_of_day) 

凡Rawscore有多個值atributes爲根據:channel_string表中的哈希值。

我應該怎麼辦?任何寶石可以幫助「will_paginate」或「kaminari」?

更新(這是我的Rawscore模型)

class Rawscore 
    include Mongoid::Document 
    include Mongoid::Timestamps::Created #for created_at field 

    belongs_to :channel 
    belongs_to :project 
    field :values, type: Hash 
    validates :values, :presence => true 

end 

我想以顯示鍵值:在Rawscore模型值散列。

回答

1

雷可以分頁陣列,但它不是有效的,因爲活動記錄將會被實例化所有檢索的記錄(即,如果你有10000條記錄,並希望只顯示100,會出現內存10000個AR對象)

Kaminari.paginate_array(my_array_object).page(params[:page]).per(10) 

相反,你可以考慮加入你的查詢和使用查詢

scores_today = Rawscore.where(
    :channel_string => ["Facebook", "Youtube", "Twitter", "Influence"], 
    :project => @project, :created_at.gt => Time.now.beginning_of_day 
).page(params[:page]) 
+0

我需要顯示各類型Rawscore的一個屬性。我如何實現這一目標? –

+0

所有屬性都在Hash of Rawscore模型中。我如何顯示它們? –

+0

請用您嘗試顯示模型的代碼更新您的問題。我不明白你想要什麼。 – MikDiet

相關問題