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
我希望用户能够过滤具有特定设施的地点。
使用 joins
和 where
非常简单
my_places = Place.joins(:facility).where(facilities: {name: 'blackboard'})
Place.includes(:facilities).where(facilities: { attribute: "specific" })
如何通过关系在 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
我希望用户能够过滤具有特定设施的地点。
使用 joins
和 where
my_places = Place.joins(:facility).where(facilities: {name: 'blackboard'})
Place.includes(:facilities).where(facilities: { attribute: "specific" })