如何对多个模型的记录进行分页? (我需要多态连接吗?)

发布于 2024-12-12 14:52:32 字数 495 浏览 3 评论 0原文

经过相当多的搜索,我仍然有点迷失。还有其他一些类似的问题涉及对多个模型进行分页,但它们要么没有答案,要么分别对每个模型进行分页。

我需要立即对帐户的所有记录进行分页。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

谁对谁错谁最难过 2024-12-19 14:52:33
emails = account.emails
tasks = account.tasks
notes = account.notes

@records = [emails + tasks + notes].flatten.sort_by(&:updated_at).reverse

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

就是这样... :)

emails = account.emails
tasks = account.tasks
notes = account.notes

@records = [emails + tasks + notes].flatten.sort_by(&:updated_at).reverse

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

Thats it... :)

木有鱼丸 2024-12-19 14:52:32

使用 will_paginate :

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

然后执行以下操作:

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

然后在您看来:

<%=will_paginate @records%>

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%>
红玫瑰 2024-12-19 14:52:32

好问题......我不确定一个“好的”解决方案,但是你可以在 ruby​​ 中做一个 hacky 的解决方案:

你需要首先取出每种类型“东西”的 30 个最新的,然后将它们放入一个由created_at索引的数组,然后按created_at对该数组进行排序并取前30个。

完全非重构的开始可能是这样的:

emails = Account.emails.all(:limit => 30, :order => :created_at)
tasks = Account.tasks.all(:limit => 30, :order => :created_at)
notes = Account.notes.all(:limit => 30, :order => :created_at)
thing_array = (emails + tasks + notes).map {|thing| [thing.created_at, thing] }
# sort by the first item of each array (== the date)
thing_array_sorted = thing_array.sort_by {|a,b| a[0] <=> b[0] }
# then just grab the top thirty
things_to_show = thing_array_sorted.slice(0,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:

emails = Account.emails.all(:limit => 30, :order => :created_at)
tasks = Account.tasks.all(:limit => 30, :order => :created_at)
notes = Account.notes.all(:limit => 30, :order => :created_at)
thing_array = (emails + tasks + notes).map {|thing| [thing.created_at, thing] }
# sort by the first item of each array (== the date)
thing_array_sorted = thing_array.sort_by {|a,b| a[0] <=> b[0] }
# then just grab the top thirty
things_to_show = thing_array_sorted.slice(0,30)

Note: not tested, could be full of bugs... ;)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文