简体   繁体   中英

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. I am using geokit-rails for this and it works great using the simple:

.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.

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.

Is there any way to do this, or will I have to make 2 db calls and merge the results?

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM