Code Monkey home page Code Monkey logo

search_by_location's Introduction

Search by location

Using ActiveRecord and geocoder

Check out the sample.

Motivation

Location data is stored in a separate model, but many models need to be filtered through the location model. Some have indirect associations with the location model, so I needed a scope in those models to abstract this. As en example consider the following association:

  ITEM -- n..1 --> STORE -- 1..1 --> LOCATION

And all the items near a location need to be filtered. It has to be through a scope so other scopes can be chained.

The gist

Basically an ActiveRecord query, found it and tweaked it from this StackOverflow answer.

  # app/models/item.rb
  # ...
  scope :near, (lambda do |location, distance|
    Item.includes(store: :geo_location)
        .references(:geo_location)
        .merge(GeoLocation.near(location, distance))
  end)
  # ...

Also noteworthy

This example is reasonably tested, don't forget to check out the spec directory, and write some tests for your production code

Contributing

Feel free to open a PR, Issue, or contact me, to suggest improvements or discuss any problems, errors or opinions.

search_by_location's People

Contributors

perezperret avatar dependabot[bot] avatar

Stargazers

清澈的海水(刘清硕) avatar

Watchers

James Cloos avatar  avatar

search_by_location's Issues

Prevent duplicate locations

Each geo_location has a unique google place_id, so duplicates could be prevented, if for instance, two stores are marked as located in 'New York, NY', the best way I figured is to relay this responsibility to controllers, which isn't ideal but should work fine. A uniqueness validator is added to geo_location's google_place_id

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.