Rails - 在 has_many 上使用连接模型过滤

Rails - filter using join model on has_many through

如何通过关系在 table 地点为 link has_many 的设施添加过滤器?

这是所有模型的样子:

class Place < ApplicationRecord
  has_many :facilities, through: :place_facilities
  has_many :place_facilities, dependent: :destroy

class PlaceFacility < ApplicationRecord
  belongs_to :place
  belongs_to :facility
end

class Facility < ApplicationRecord
  has_many :places, through: :place_facilities
  has_many :place_facilities
end

我希望用户能够过滤具有特定设施的地点。

使用 joinswhere

非常简单
my_places = Place.joins(:facility).where(facilities: {name: 'blackboard'})
Place.includes(:facilities).where(facilities: { attribute: "specific" })

有关详细信息,请参阅 https://guides.rubyonrails.org/active_record_querying.html#specifying-conditions-on-eager-loaded-associations