分页而不是事前获取对象? (路轨+ Kaminari)

编程入门 行业动态 更新时间:2024-10-25 16:22:07
本文介绍了分页而不是事前获取对象? (路轨+ 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)

本文发布于:2023-06-13 04:30:36,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/674147.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:路轨   分页   事前   而不是   对象

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!