mysql - Rails sort child model based on parent model distance calculation -


i have product model , shop model. relationship between 2 shop has_many products , products belongs_to shop.

the shop model has 2 fields longitude , latitude used distance calculation using geokit-rails. have been successful in sorting shops nearest distance given longitude , latitude using:

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

the problem products. products needs sorted according nearest shop location well. have searched through , found out child model can sorted parents attributes this:

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

the order function works if supplied model field. how can sort products calculating shop distance.

please help.

have @ documentation on using :through - should need.

so product like:

class product < activerecord::base   belongs_to :shop   acts_as_mappable through: :shop   ... end 

and query like:

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

Comments

Popular posts from this blog

javascript - Clear button on addentry page doesn't work -

c# - Selenium Authentication Popup preventing driver close or quit -

tensorflow when input_data MNIST_data , zlib.error: Error -3 while decompressing: invalid block type -