1

我正在尝试在我的 Rails 项目中为 Kaminari 分页添加条件,但我发现这几乎是不可能的。这是我的代码

  UserController
#feed=Feed.order('created_at desc').page(params[:page], :conditions=>["source=? or        source=?","beep","sub_beep"])

以上不起作用所以我尝试了以下

  @feed1 = Feed.find(:all,:conditions=>["source=? or source=?","beep","sub_beep"])
  @feed= Kaminari.paginate_array(@feed1).page(params[:page]).per(2)

以上显示内容,但每页不显示 2 个内容,并且显示的元素没有排序。

我需要一种方法来为 kaminari 分页添加条件并对结果进行排序

请我需要帮助!:(

4

1 回答 1

2

Kaminari 旨在仅与 ActiveRecord 3+ 样式的查询界面一起使用。

请避免使用:conditions迟早会弃用的“Hasheritis”语法,而是使用where方法链。

whereRails 指南中详细记录了该方法的用法:http: //guides.rubyonrails.org/active_record_querying.html#conditions

你的控制器代码应该是这样的:

feeds = Feed.where(:source => ['beep', 'sub_beep']).order('created_at desc').page(params[:page])
于 2012-09-24T08:38:32.077 回答