1

I have a project where users can search for results within a specified distance of their location, e.g. 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

Dave Essery
  • 207
  • 1
  • 12

1 Answers1

0

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

Javier Olan
  • 560
  • 7
  • 14