分页而不是事前获取对象? (路轨+ Kaminari) [英] Fetch objects upon pagination, rather than beforehand? (Rails + Kaminari)

查看:77
本文介绍了分页而不是事前获取对象? (路轨+ Kaminari)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我有一个搜索控制器,该控制器返回对象ID的数组(在本例中为用户").

So I have a search controller that returns a array of Object IDs (in this case 'Users').

由于搜索结果数组可以包含数百个ID,所以我只想在需要将它们发送到视图时才获取实际的对象本身.简而言之,我想使用分页为每个ID块(即1..10、11..20等)预先获取对象本身,而不是让视图逻辑获取对象.我不知道该怎么做到.

Since the search results array can contain hundreds of IDs, I'd rather fetch the actual Objects themselves only when I need to send them to the view. In short, I want to use pagination to fetch the Objects themselves beforehand for each chunk of IDs (i.e. 1..10, 11..20, etc), rather than have the view logic fetch the Objects. I have no idea how to accomplish this.

我的控制器代码

users = [1,3,456,23423,234,23...]
#somehow have the pagination get the Objects from DB for each page
@persons = Kaminari.paginate_array(users).page(params[:page]).per(10)

查看

- @persons.each do |person| #I WANT TO HAVE THE FULL WRITER OBJECT BY NOW
  = person.name             # instead of doing a 'User.find(person.id)' in the view

有什么想法吗?

提前谢谢!

推荐答案

在网址中输入页码参数.然后,如果您要显示每页10张,并说用户单击了第三页,则将页码减去一页(10),然后再添加一页给您21.这是数据库查询的起点.并限制为10个结果

put a page number parameter in the url. Then if you want to display 10 per page and say the user clicks page three, subtract page number by one page (10) and add one giving you 21. this is your start for the database query. and limit to 10 results

这篇关于分页而不是事前获取对象? (路轨+ Kaminari)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆