使用分页显示来自两个不同模型的记录

时间:2014-10-10 12:44:06

标签: ruby-on-rails will-paginate

我有两个型号.. videoyoutube_video ....我有一个index页面来显示两个视频..所以我添加两个结果集在一个数组中显示在索引页面上,但我得到分页错误.. undefined method total pages for array ....我做错了什么,,,我知道有一个解决方法,因为paginate将在{{1而不是ActiveRecord Relation ..我甚至尝试过model.where ......但是没有用......任何解决方法都会有所体会:)

我的控制器..

ActiveRecord

我的观看文件

@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 =@videos + @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> 。它有效,但在合并与另一个模型之后......会打破......有什么想法吗?

1 个答案:

答案 0 :(得分: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中对数组进行排序。

相关问题