因此,我有一个搜索控制器,该控制器返回对象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)
发布评论