我正在使用Rails5。我有以下模型...
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'))