我有两个模型..video
和youtube_video
....我有一个index
页面来显示这两个视频..所以我我在一个数组中添加两个结果集以显示在索引页上,但我收到分页错误..as 数组的未定义方法总页数
....我做错了什么,,,我知道有一个解决方法,因为分页适用于 ActiveRecord Relation 而不是 ActiveRecord ..我什至尝试过 model.where...但不起作用.. .对此的任何解决方法将不胜感激:)
我的 Controller ..
@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
我的 View 文件
###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。
。它可以工作,但是在与另一个模型合并之后......它会破坏......有什么想法吗?
最佳答案
您要对每个集合进行分页,您需要先添加它们,然后对数组进行分页。
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 中对数组进行排序。
关于ruby-on-rails - 使用分页显示来自两个不同模型的记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26299740/