我正在从另一个仓库分叉一个仓库,作为客户的独立仓库,无论出于何种原因,我的大脑都冻结了。
希望将特定查询设置为仅命中这个区域:西南,然后是所有关联的位置。
所以很自然的模型结构是:
Region
has_many :locations
Location
belongs_to :region
我正在考虑更新一些代码,以便在一些助手中专门针对西南地区,但它太笨重了。
def southwest_general
Region.where(name: 'Southwest')
end
def southwest_locations
Location.where(region_id: 5)
end
def all_locations
southwest_locations.all.map do |loc|
pin = { icon: pin_path(loc.region_id) }
loc.attributes.merge(pin)
end
end
我真的在考虑更新 Southwest_locations 方法以通过 Locations 命中 Region 模型。我以为我可以做点什么:
def southwest_locations
Location.includes(:regions).where('regions.name =?', 'Southwest').references(:regions)
end
但这实际上更像是搜索而不是数据库查询,对吗?
特定地区的位置
Location.joins(:region).where(regions: {id: 5})
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句