[英]Using Geokit-Rails, How can I find all fields within a distance OR have a bit value set to 1
I have a project where users can search for results within a specified distance of their location, eg within 10 miles of their postcode. 我有一个项目,用户可以在其位置的指定距离内(例如其邮政编码的10英里之内)搜索结果。 I am using geokit-rails for this and it works great using the simple: 我为此使用geokit-rails,使用简单的方法效果很好:
.within(@distance, :origin => @latlng)
However there are some results that are not dependant on location so I would like them to always appear in the search, this will be denoted by a bit property called 'remote' set to 1 if it should be included. 但是,有些结果与位置无关,因此我希望它们始终出现在搜索中,这将由称为'remote'的位属性(如果应包含在内)设置为1来表示。
So what I need to do is be able to search for all results where the Distance is less than X OR remote equals 1. 因此,我需要做的是能够搜索距离小于X或远程等于1的所有结果。
Is there any way to do this, or will I have to make 2 db calls and merge the results? 有什么方法可以执行此操作,还是我必须进行2次数据库调用并合并结果?
Many thanks 非常感谢
distance attribute was removed from the lib, but there are some workaround about how include again, already included can add a condition for your attribute distance with the attribute remote. distance属性已从库中删除,但是有一些变通方法,关于如何再次包含,已经包含可以为您的属性与remote属性的距离添加条件。
module Geokit::ActsAsMappable
module ClassMethods
# Overwrite Geokit `within`
def within(distance, options = {})
options[:within] = distance
# Have to copy the options because `build_distance_sql` will delete them.
conditions = distance_conditions(options.dup)
sql = build_distance_sql(options)
self.select(
"#{sql} AS #{self.distance_column_name}, #{table_name}.*"
).where(conditions)
end
# Overwrite Geokit `by_distance`
def by_distance(options = {})
sql = build_distance_sql(options)
self.select("#{sql} AS #{self.distance_column_name}, #{table_name}.*"
).order("#{self.distance_column_name} ASC")
end
private
# Copied from geokit-rails/lib/geokit-rails/acts_as_mappable.rb
# Extract distance_sql building to method `build_distance_sql`.
def build_distance_sql(options)
origin = extract_origin_from_options(options)
units = extract_units_from_options(options)
formula = extract_formula_from_options(options)
distance_sql(origin, units, formula)
end
end
end
https://github.com/geokit/geokit-rails/issues/56 https://github.com/geokit/geokit-rails/issues/56
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.