gpt4 book ai didi

sql - 分页包含变量

转载 作者:数据小太阳 更新时间:2023-10-29 08:43:00 26 4
gpt4 key购买 nike

我正在构建一个包含群组 > 帖子 > 评论的应用为了减少 SQL 请求的数量,我使用了 includes 方法

# group controller
def show
@posts = @group.posts.includes(:comments)
end

现在,我想对评论进行分页。但是我不知道如何使用 gem will_paginate 中的 .paginate 函数

你有什么建议吗?

最佳答案

Controller 内部的分页问题

由于您想要与单个帖子关联的分页评论,因此在 Controller 中实现起来很复杂,因为您需要创建 N 个分页评论对象(假设您有 N 个帖子)。

你能做什么

1. 像往常一样加载包含评论的帖子,以减少过多的查询。但是不要在这里运行分页

2.仅在 View 内实现分页

<%= @posts.each do | post | %>
<% comments = post.comments.paginate(page: params[:page]) %>

..
...
...
<%= will_paginate comments%>
<% end %>

关于sql - 分页包含变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46809415/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com