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 有一个具有多个值的哈希作为表的属性,具体取决于: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 模型中显示 :values Hash 的键值。

4

2 回答 2

1

Kaminari 可以对数组进行分页,但效率不高,因为 Active Record 将实例化所有检索到的记录(即,如果您有 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])
于 2013-05-31T08:40:47.557 回答
0

问题的答案在于使用 WillPaginate::Collection 创建自己的分页结构。

这是 ruby​​doc 链接:http ://rubydoc.info/github/mislav/will_paginate/frames

于 2013-06-11T07:53:25.757 回答