将对多个表进行分页

发布于 2024-12-03 14:39:45 字数 952 浏览 0 评论 0原文

我有下表:

  1. 用户
  2. 帐户(用户 has_one 帐户)
  3. 图片(用户 has_many 图片)

我想显示用户及其相关帐户和图片。这是一个典型的 N+1 查询问题,我使用 急切加载。

User.includes(:account, :pictures).all

我正在尝试使用 will_paginate gem。如何将这种急切加载机制包含到 will_paginate 中?

我在这里发现了类似的问题:将 will_paginate 与多个模型 (Rails) 一起使用。但答案中的链接不合适,而且答案本身也很旧。此外,它使用原始 sql 查询,这是我不喜欢的。

我想要的输出如下:

user.name account.field1 account.field2 picture.count
abc1       abcf1          abcf2           4
zxc        zxcf1          zxcf2           7

所以基本上当我执行 user.name 时,它不会执行查询,而只是从变量中获取数据; 同样,当我执行 user.account.field1 时,它应该从变量中获取它而不是执行查询。

I have following tables :

  1. users
  2. accounts ( user has_one account )
  3. pictures ( user has_many pictures )

I want to display a User along with its related account and pictures. This is a typical N+1 queries problem and I solved it using eager loading.

User.includes(:account, :pictures).all

I am trying to use will_paginate gem. How do I include this eager loading mechanism into will_paginate ?

I found a similar question here: Using will_paginate with multiple models (Rails). But the links in the answer are out of place and the answer itself is quite old. Furthermore, it uses raw sql queries, which I don't prefer.

I want the output as follows :

user.name account.field1 account.field2 picture.count
abc1       abcf1          abcf2           4
zxc        zxcf1          zxcf2           7

So basically when I do user.name , it will not execute a query but just get the data from the variables;
same way when I do user.account.field1 it should fetch it from the variable and not execute a query.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

挽清梦 2024-12-10 14:39:45

paginate 方法与 all 方法类似。因此,您可以简单地执行以下操作:

User.includes(:account, :pictures).paginate(
  :page => params[:page], :per_page => 30)

OR

User.paginate(:include=> [:account, :pictures], 
  :page => params[:page], :per_page => 30)

注意:

will_paginate gem 在 Array 类上提供了一个方便的方法分页:

[1,2,3,4].paginate(:page => 1, :per_page=>2)

The paginate method is similar to all method. So you can simply do this:

User.includes(:account, :pictures).paginate(
  :page => params[:page], :per_page => 30)

OR

User.paginate(:include=> [:account, :pictures], 
  :page => params[:page], :per_page => 30)

Note:

The will_paginate gem provides a convenience method on the Array class for pagination:

[1,2,3,4].paginate(:page => 1, :per_page=>2)
顾北清歌寒 2024-12-10 14:39:45

我最终在数组上使用分页而不是 gem 本身。

    current_page = Integer(params[:page]||1)
    per_page = 250
    @users_r = User.all(:conditions=>"some condition",:limit=>per_page,:offset=>per_page*(current_page-1),:include=>[:account,:user_info ,:pictures])
    count = @users_r.length
    @users = WillPaginate::Collection.create(current_page, per_page, count) do |user|
      user.replace(@users_r)
    end
    @users.paginate(params)

I finally used pagination on a array rather than the gem itself.

    current_page = Integer(params[:page]||1)
    per_page = 250
    @users_r = User.all(:conditions=>"some condition",:limit=>per_page,:offset=>per_page*(current_page-1),:include=>[:account,:user_info ,:pictures])
    count = @users_r.length
    @users = WillPaginate::Collection.create(current_page, per_page, count) do |user|
      user.replace(@users_r)
    end
    @users.paginate(params)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文