Milind
Milind

Reputation: 5112

show records from two different models using pagination

i have two models ..video and youtube_video....i have a index page to show both videos..so i am adding both resultset in one array to show on index page but i am getting pagination error..as undefined method total pages for array....what i am doing wrong,,,i know theres a work around as will paginate works on ActiveRecord Relation and not on ActiveRecord..i even tried model.where...but doesnt works...any workaround on this will be appreciate :)

my 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

my view file

 ###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>

if i use just @videos..it works but after merging with another model..it breaks..any ideas?

Upvotes: 2

Views: 687

Answers (1)

j-dexx
j-dexx

Reputation: 10406

You're paginating each collection, you'll need to add them first then paginate the array.

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)

As you're adding two queries together it looks like you'll have to sort the array in ruby.

Upvotes: 2

Related Questions