Ruby Rails:如何创建一个范围来检查是否有附件?

Ruby on Rails: How can I create a scope that checks whether there is an attachment?

我通过活动存储有一个带有“海报”附件的电影对象 (has_one_attached :poster)

我想创建一个范围来检索所有没有附上海报的电影,但是这在这里不起作用:

scope :no_poster, -> { where (poster: nil) }

它导致错误说

Unknown column 'movies.poster' in 'where clause'

(这对我来说很有意义:)

如何创建这样的范围?

Rails 6.1 adds 缺少在 ActiveRecord 中搜索孤立记录的查询方法。

尝试

scope :no_poster, -> { where.missing(:poster_attachment) }

如果有人搜索此内容,请在这里回答我自己的问题:engineersmnky 的评论是正确的,关系名称是 poster_attachment,所以为了创建一个搜索电影的范围,没有海报是

scope :no_poster, -> { where.missing(:poster_attachment) }