mysqlruby-on-railssortinggeokit

Rails sort child model based on parent model distance calculation


I have a product model and a shop model. The relationship between two is shop has_many products and products belongs_to shop.

The shop model has two fields longitude and latitude used for distance calculation using geokit-rails. I have been successful in sorting shops by nearest distance to any given longitude and latitude using:

Shop.by_distance(origin:[params[:latitude], params[:longitude]]).sort_by{|s| s.distance_to([params[:latitude], params[:longitude]])}

The problem is with products. The products needs to be sorted according to nearest shop location as well. I have searched through and found out that a child model can be sorted from parents attributes like this:

Product.joins(:shop).order('shops.name')

The order function works only if supplied with model field. How can I sort products calculating shop distance.

Please help.


Solution

  • Have a look at the documentation on using :through - this should be exactly what you need.

    So Product would look like:

    class Product < ActiveRecord::Base
      belongs_to :shop
      acts_as_mappable through: :shop
      ...
    end
    

    And your query would be something like:

    Product.joins(:shop).by_distance(origin: [params[:latitude], params[:longitude]])