我有两个相关的轨道型号
class Location < ActiveRecord::Base
has_many :rates
geocoded_by ....
end
class Rate < ActiveRecord::Base
belongs_to :location
end
我正在使用地理编码宝石-http://www.rubygeocoder.com
我想找到与用户相距一定距离内的具有特定pair
属性的所有费率。
在SQL中,我会对利率进行内部连接
SELECT * FROM rates INNER JOIN locations ON rates.location_id=locations.id;
然后,在对属性上插入一个where条件进行过滤,然后在结果表上使用Geocoder的near方法(near方法通过在查询中插入where条件来工作,该查询使用位置上的纬度和经度属性来计算距离),只选择正确距离内的行
我怎样才能在铁轨上做到这一点?我试过
rates = Rate.joins(:locations)
我得到
ActiveRecord::ConfigurationError: Association named 'locations' was not found; perhaps you misspelled it?
我想做这个
rates = Rate.joins(:locations).near(my_latitude, my_longitude, distance).where(:rates => {:pair => 'xxxx'})
但我有
undefined method `near' for #<ActiveRecord::Relation:0xa075ff8>
near = Location.near(location, radius)
Rate.includes(:location).references(:location).merge(near)
这可与其他位置或速率范围链接
我知道这是一个旧问题,但仍然没有答案,无处可答:)
我遇到了同样的问题,然后在地理编码文档中偶然发现了一个小信息,它说为了使外部联接工作(在rails中使用includes来完成),你应该将联接与:select结合使用,这对我来说很好(我需要从某个位置的用户那里找到所有产品)。
因此,就我的想法而言,这可能适用于您的情况:
rates = Location.near(my_latitude, my_longitude, distance, :select => "rates.*").joins(:locations).where(:rates => {:pair => 'xxxx'})
我希望这能有所帮助。