我如何编写一个Rails finder方法来搜索has_many没有特定属性的模型?

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

我正在使用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”怎么办?

ruby-on-rails ruby-on-rails-5 finder
1个回答
0
投票

这将给所有没有JPG或没有照片的城市>

City.where.not(id: Photo.select(:city_id).where(extension: 'JPG'))
© www.soinside.com 2019 - 2024. All rights reserved.