Ruby on rails 这个代码有什么问题
尝试: 通过将方法中的第一行更改为:Ruby on rails 这个代码有什么问题,ruby-on-rails,ruby,Ruby On Rails,Ruby,尝试: 通过将方法中的第一行更改为: class CommentsController < ApplicationController def users_comments posts = Post.all comments = posts.map(&:comments).flatten @user_comments = comments.select do |comment| comment.author.username =
class CommentsController < ApplicationController
def users_comments
posts = Post.all
comments = posts.map(&:comments).flatten
@user_comments = comments.select do |comment|
comment.author.username == params[:username]
end
end
end
像
代码中缺少do的结尾,您还需要包含注释和作者关联,以避免n+1查询
class CommentsController < ApplicationController
def users_comments
posts = Post.includes(comments: [:user]).all
comments = posts.map(&:comments).flatten
@user_comments = comments.select do |comment|
comment.author.username == params[:username]
end
end
所以代码看起来像
posts = Post.includes(comments: :author)
我不知道该结束了吗think@Sachin当前位置为什么不发布错误消息?我们不知道。你为什么不告诉我们,这样我们就可以帮你修好它?问题是什么?你收到错误信息了吗?如果是,是什么?你收到警告信息了吗?如果是,是什么?代码太慢了吗?如果是,需要多快,现在需要多快?它是否占用了太多内存?如果是,它可以使用多少,现在使用多少?它是不可维护的吗?它是否给出了错误的结果?如果是,你期望得到什么样的结果?为什么?你得到了什么样的结果?为什么这个结果不是你期望的结果?请完整、准确地描述……代码的功能,包括任何和所有角落情况、特殊情况、边缘情况和例外情况,以及为什么、实际功能、您收到的任何和所有错误消息和警告,以及一组输入和期望输出的示例,这些示例详尽地展示了所有边缘情况、角点情况、特殊情况和异常。或者,简而言之,请提供一个。
posts = Post.includes(comments: :author)
class CommentsController < ApplicationController
def users_comments
posts = Post.includes(comments: :author)
comments = posts.map(&:comments).flatten
@user_comments = comments.select do |comment|
comment.author.username == params[:username]
end
end
end