2014-10-10 86 views
2

我有两个型号.. videoyoutube_video两种不同的模式记录....我有一个index页面同时显示videos..so我是加入结果集都在一个阵列上显示指数页面,但我得到分页错误..因为undefined method total pages for array ....我做错了什么,,,我知道theres工作,将分页作品ActiveRecord Relation而不是ActiveRecord ..我甚至尝试model.where ...但犯规的作品......在这个什么解决办法将欣赏:)显示使用分页

我的控制器..

@videos = Video.includes(:user,:reputations,:tags,:comments).paginate(:page =>params[:page], :per_page => 10).order("created_at DESC") 
@youtube_videos=YoutubeVideo.includes(:user,:tags).paginate(:page =>params[:page], :per_page => 10).order("created_at DESC") 
@all_videos [email protected] + @youtube_videos 

我查看文件

###looping on @all_videos.... 
<div class="page_info text-center" style="margin-bottom:10px"> 
<!-- it breaks here --> 
    <%= page_entries_info @all_videos %> 
<hr> 
<!-- it breaks here too if i remove above line-->    
<%= ajax_will_paginate @all_videos %> 
</div> 

,如果我只使用@videos.。它的工作原理,但合并与另一model..it breaks..any想法后?

回答

2

您正在为每个集合分页,您需要先添加它们然后对该数组进行分页。

require 'will_paginate/array' 
@videos = Video.includes(:user,:reputations,:tags,:comments).order("created_at DESC") 
@youtube_videos=YoutubeVideo.includes(:user,:tags).order("created_at DESC") 
@all_videos = (@videos + @youtube_videos).paginate(:page =>params[:page], :per_page => 10) 

当你将两个查询加在一起时,它看起来像你必须在ruby中排序数组。

+0

ok ...我得到了我错在哪里..谢谢... @japed – Milind 2014-10-10 13:20:21

+0

你的例子不是很好,添加两个数组都不会将它们排序在彼此之内。在调用.paginate – Alain 2015-03-12 18:34:44

+0

之前,您错过了.sort我刚刚重用了他的代码。你是对的,但我只是回答有关分页错误的问题。 – 2015-03-13 09:18:28