如何编写 Rails 查找器方法来搜索 has_many 没有特定属性的模型?

How do I write a Rails finder method to search for my model where the has_many has no particular attribute?

我正在使用 Rails 5. 我有以下型号 ...

class City < ApplicationRecord
    ...
    has_many :photos, :autosave => true, :dependent => :destroy, :as => :photo, :inverse_of => :photo

我意识到如果我想搜索没有照片的城市,我可以写这个取景器...

City.includes(:photos).where(photos: { city_id: nil })

但是如果我想搜索没有照片且照片扩展属性为 "JPG" 的城市怎么办?

这将给出所有没有JPG或没有照片

的城市
City.where.not(id: Photo.select(:city_id).where(extension: 'JPG'))