1

I'd like to make a newsfeed for the homepage of a site i'm playing around with. There are two models: Articles, and Posts. If I wanted just one in the newsfeed it would be easy:

 @newsfeed_items = Article.paginate(:page => params[:page])

But I would like for the two to be both paginated into the same feed, in reverse chronological order. The default scope for the article and post model are already in that order.

How do I get the articles and posts to be combined in to the newsfeed as such?

Thanks!

EDIT: What about using SQL in the users model?

Just wondering: maybe would it be possible define in User.rb:

 def feed
    #some sql like (SELECT * FROM articles....)
 end

Would this work at all?

jay
  • 12,066
  • 16
  • 64
  • 103

5 Answers5

3

in my last project i stuck into a problem, i had to paginate multiple models with single pagination in my search functionality. it should work in a way that the first model should appear first when the results of the first model a second model should continue the results and the third and so on as one single search feed, just like facebook feeds. this is the function i created to do this functionality

def multi_paginate(models, page, per_page)

  WillPaginate::Collection.create(page, per_page) do |pager|

    # set total entries
    pager.total_entries = 0
    counts = [0]
    offsets = []
    for model in models
          pager.total_entries += model.count
          counts << model.count
          offset = pager.offset-(offsets[-1] || 0)
          offset = offset>model.count ? model.count : offset 
          offsets << (offset<0 ? 0 : offset)
    end

    result = []
    for i in 0...models.count
          result += models[i].limit(pager.per_page-result.length).offset(offsets[i]).to_a
    end

    pager.replace(result)
  end

end

try it and let me know if you have any problem with it, i also posted it as an issue to will_paginate repository, if everyone confirmed that it works correctly i'll fork and commit it to the library. https://github.com/mislav/will_paginate/issues/351

Emad Elsaid
  • 323
  • 3
  • 19
2

for those interested, please check this question: Creating a "feed" from multiple rails models, efficiently?

Here, Victor Piousbox provides a good, efficient solution.

Community
  • 1
  • 1
jay
  • 12,066
  • 16
  • 64
  • 103
1

Look at paginate_by_sql method. You can write unione query to fetch both articles and posts:

select 'article' as type, id from articles
  union
select 'post' as type, id from posts
BenMorel
  • 34,448
  • 50
  • 182
  • 322
Adrian Serafin
  • 7,665
  • 5
  • 46
  • 67
  • this idea seems to have merit. However, how would I implement it (still an SQL noob). I have a Users controller (and model).. do I write a method in there like self.newsfeed(user) #code here end? any pointers would be much appreciated – jay Aug 30 '11 at 19:36
0

You can paginate both if you use AJAX. Here is well explained how to paginate using AJAX with WillPaginate.

cicloon
  • 1,099
  • 5
  • 13
0

You can paginate an array using WillPaginate::Collection.create. So you'd need to use ActiveRecord to find both sets of data and then combine them in a single array.

Then take a look at https://github.com/mislav/will_paginate/blob/master/lib/will_paginate/collection.rb for documentation on how to use the Collection to paginate any array.

Mike Gorski
  • 1,228
  • 1
  • 8
  • 13