带附件的评论计数。使用条件测试记录是否具有活动存档附件

问题描述 投票:0回答:1

我正在学习二进制文件,我有一个我已经疯狂了好几天的问题,我创建了一个带有 2 个表的应用程序: 帖子和子评论表,在评论表中我有一个附加字段 has_one 使用活跃存款进行。 我只想计算带有附件的评论,并在帖子索引页面上显示的帖子表中显示计数,如下所示:

this is what I would like to achieve

# models
class Comment< ApplicationRecord
belongs_to :Post
has_one_attached :image

在评论视图中我可以成功使用以下条件

# show comment
<% if comment.image.attached? %>

 <%end%>

但是我无法让这个条件在我写的帖子的索引视图中工作

<% if post.comment.image.attached? %>

 <%end%>

我哪里错了

ruby-on-rails attachment rails-activestorage
1个回答
0
投票

这么多天后,我不得不写我的第一个问题关于 stack_overflow 来找到解决方案,而且这篇文章也很微不足道,也许对其他人有用

我所要做的就是将其插入到我的索引表中:

<%= post.comments.joins(:image_attachment).count %>

我缺少使用 active_storage 或将 _attachment 添加到图像字段的约定

:image_attachment

这篇文章帮助我理解了

现在一切正常了

© www.soinside.com 2019 - 2024. All rights reserved.