下面的查询作为原始sql运行良好,但是由于它访问了2个模型,我不确定如何在活动记录中构造它.
sql = "SELECT spots.*
FROM spots, areas
WHERE areas.area = '#{@area.area}'
AND shape && lonlat
AND ST_Contains(shape,lonlat);"我怎么才能着手建造这个呢?这不是加入所以..。
谢谢!
发布于 2015-09-10 03:08:45
您可以以这种方式生成相同的SQL:
Spot.from("spots, areas")
.where("areas.area = ?",@area.area)
.where("shape && lonlat")
.where("ST_Contains(shape,lonlat)")但这样,您就不会使用ActiveRecord最喜欢的东西,比如热切的负载和关系。为了获得相同的结果,您可以这样做:
在模型中:
Spot < ActiveRecord::Base
belongs_to :area
end
Area < ActiveRecord::Base
end而问题是:
Spot.joins(:area).where(areas: {area: @area.area})https://stackoverflow.com/questions/29034941
复制相似问题