将两个模型中的数据分页到一个新闻源中:Ruby on Rails 3 // Will_paginate
我想为我正在使用的网站的主页制作一个新闻源。有两种模型:文章和帖子。如果我只想要新闻源中的一个,那就很容易了:
@newsfeed_items = Article.paginate(:page => params[:page])
但我希望这两个都以相反的时间顺序分页到同一个源中。文章和帖子模型的默认范围已按该顺序排列。
如何将文章和帖子合并到新闻源中?
谢谢!
编辑:在用户模型中使用 SQL 怎么样?
只是想知道:也许可以在 User.rb 中定义:
def feed
#some sql like (SELECT * FROM articles....)
end
这会起作用吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
在我的上一个项目中,我遇到了一个问题,我必须在搜索功能中使用单分页对多个模型进行分页。它应该以这样的方式工作:当第一个模型的结果时,第一个模型应该首先出现,第二个模型应该继续结果,第三个等等作为单个搜索源,就像 Facebook 源一样。这是我创建的用于执行此功能的函数,
尝试一下,如果您有任何问题,请告诉我,我还将其作为问题发布到 will_paginate 存储库,如果每个人都确认它工作正常,我将 fork 并将其提交到图书馆。 https://github.com/mislav/will_paginate/issues/351
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
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
对于那些感兴趣的人,请检查这个问题:从多个Rails 模型,高效吗?
在这里,Victor Piousbox 提供了一个良好、高效的解决方案。
for those interested, please check this question: Creating a "feed" from multiple rails models, efficiently?
Here, Victor Piousbox provides a good, efficient solution.
查看 paginate_by_sql 方法。您可以编写 unione 查询来获取文章和帖子:
Look at paginate_by_sql method. You can write unione query to fetch both articles and posts:
如果您使用 AJAX,则可以对两者进行分页。 这里很好地解释了如何使用 AJAX 和 WillPaginate 进行分页。
You can paginate both if you use AJAX. Here is well explained how to paginate using AJAX with WillPaginate.
您可以使用 WillPaginate::Collection.create 对数组进行分页。因此,您需要使用 ActiveRecord 查找两组数据,然后将它们合并到一个数组中。
然后看看 https://github.com/mislav/ will_paginate/blob/master/lib/will_paginate/collection.rb 有关如何使用 Collection 对任何数组进行分页的文档。
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.