Ruby on rails 4 Rails&通过分部嵌套注释,奇怪的递归

Ruby on rails 4 Rails&通过分部嵌套注释,奇怪的递归,ruby-on-rails-4,partials,Ruby On Rails 4,Partials,我为多态评论模型做了一个非常简单的设置,这个模型本身也有很多关系,叫做:回复: # comment.rb class Comment < ActiveRecord::Base belongs_to :commentable, polymorphic: true default_scope -> { order(created_at: :asc) } belongs_to :user belongs_to :comment_parent, class_name: "Co

我为多态评论模型做了一个非常简单的设置,这个模型本身也有很多关系,叫做:回复:

# comment.rb
class Comment < ActiveRecord::Base
  belongs_to :commentable, polymorphic: true
  default_scope -> { order(created_at: :asc) }
  belongs_to :user
  belongs_to :comment_parent, class_name: "Comment", foreign_key: "comment_id"
  has_many :replies, class_name: "Comment", foreign_key: "comment_id"
  validates :content, presence: true
  validates :commentable, presence: true
end
我在我的博客浏览页面上启动了部分内容,如下所示:

= render @comments
最后,注释/_注释部分包含:

简单代码,仅显示gravatar和用户名

# comment/_comment.html.haml
%ol.media-list
  %li.media{:class => ("media-replied" if comment.comment_id)}
    = link_to gravatar_for(comment.user, size: 80, class_name: 'media-object img-circle'), comment.user, class: 'pull-left'
    = render partial: 'comments/comment', collection: comment.replies
不知何故,这造成了一些奇怪的重复现象,即回复显示在正确的位置,但也显示在错误的位置。问题是这里面似乎没有任何逻辑。至少对我来说不是

我已经检查了控制台,以验证数据库是否包含记录之间的正确关系没有错误记录,因此它必须在表示/部分调用中


有什么想法吗?

好吧,那当然是我自己的愚蠢错误。。。调用@commentable.comments.all结果是所有注释,因此即使是嵌套注释也会显示为顶级注释

短期通过添加wherecomment_id:nil来修复它

# comment/_comment.html.haml
%ol.media-list
  %li.media{:class => ("media-replied" if comment.comment_id)}
    = link_to gravatar_for(comment.user, size: 80, class_name: 'media-object img-circle'), comment.user, class: 'pull-left'
    = render partial: 'comments/comment', collection: comment.replies