Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to paginate records from multiple models? (do I need a polymorphic join?)

After quite a bit of searching, I'm still a bit lost. There are a few other similar questions out there that deal with paginating multiple models, but they are either unanswered or they pagainate each model separately.

I need to paginate all records of an Account at once.

class Account
  :has_many :emails
  :has_many :tasks
  :has_many :notes
end

So, I'd like to find the 30 most recent "things" no matter what they are. Is this even possible with the current pagination solutions out there?

Like using some combination of eager loading and Kaminari or will_paginate?


Or, should I first set up a polymorphic join of all these things, called Items. Then paginate the most recent 30 items, then do a lookup of the associated records of those items.

And if so, I'm not really sure what that code should look like. Any suggestions?


Which way is better? (or even possible)

Rails 3.1, Ruby 1.9.2, app not in production.

like image 897
GoodGets Avatar asked Oct 31 '11 18:10

GoodGets


1 Answers

with will_paginate :

@records = #do your work and fetch array of records you want to paginate ( various types )

then do the following :

current_page = params[:page] || 1
per_page = 10
@records = WillPaginate::Collection.create(current_page, per_page, records.size) do   |pager|
pager.replace(@records)
end

then in your view :

<%=will_paginate @records%>
like image 199
Ahmed Samir Shahin Avatar answered Nov 15 '22 09:11

Ahmed Samir Shahin