如何对多个模型的记录进行分页? (我需要多态连接吗?)
经过相当多的搜索,我仍然有点迷失。还有其他一些类似的问题涉及对多个模型进行分页,但它们要么没有答案,要么分别对每个模型进行分页。
我需要立即对帐户的所有记录进行分页。
class Account
:has_many :emails
:has_many :tasks
:has_many :notes
end
所以,我想找到 30 个最近的“事情”,不管它们是什么。使用当前的分页解决方案是否可以做到这一点?
喜欢使用急切加载和 Kaminari 或 will_paginate 的某种组合吗?
或者,我应该首先设置所有这些东西的多态连接,称为 Items。然后对最近的 30 个项目进行分页,然后查找这些项目的关联记录。
如果是这样,我不太确定该代码应该是什么样子。有什么建议吗?
哪种方式更好? (甚至可能)
Rails 3.1,Ruby 1.9.2,应用程序未投入生产。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
就是这样... :)
Thats it... :)
使用 will_paginate :
然后执行以下操作:
然后在您看来:
with will_paginate :
then do the following :
then in your view :
好问题......我不确定一个“好的”解决方案,但是你可以在 ruby 中做一个 hacky 的解决方案:
你需要首先取出每种类型“东西”的 30 个最新的,然后将它们放入一个由created_at索引的数组,然后按created_at对该数组进行排序并取前30个。
完全非重构的开始可能是这样的:
注意:未经测试,可能充满错误...;)
Good question... I'm not sure of a "good" solution, but you could do a hacky one in ruby:
You'd need to first fetch out the 30 latest of each type of "thing", and put them into an array, indexed by created_at, then sort that array by created_at and take the top 30.
A totally non-refactored start might be something like:
Note: not tested, could be full of bugs... ;)