Rails-使用has_many上的连接模型进行过滤

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

我如何通过关系通过链接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

我希望用户能够过滤具有特定功能的位置。

ruby-on-rails has-many-through
1个回答
0
投票

[joinswhere的使用非常简单

my_places = Place.joins(:facility).where(facilities: {name: 'blackboard'})
© www.soinside.com 2019 - 2024. All rights reserved.