geokit / geokit-rails

Official Geokit plugin for Rails/ActiveRecord. Provides location-based goodness for your Rails app. Requires the Geokit gem.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

UndefinedTable: ERROR: missing FROM-clause entry for table "locations"

SunnyTam opened this issue · comments

commented

I am following the guide to build a merchant model using location model
https://github.com/geokit/geokit-rails#using-through
Below is my code

class Merchant < ActiveRecord::Base
  #location function using location model for geo-kit
  has_one :location, :as => :locatable
  acts_as_mappable :through => :location
end
class Location < ActiveRecord::Base
  belongs_to :locatable, polymorphic: true
  acts_as_mappable
end

I am trying to run Merchant.within(50,:origin => [lat,lng])

It throw

SELECT "merchants".* FROM "merchants" WHERE (locations.lat IS NOT NULL AND locations.lng IS NOT NULL) AND (locations.lat>21.66680773573204 AND locations.lat<23.11250626426795 AND locations.lng>113.42587182222651 AND locations.lng<114.9894261777735) AND ((
          (ACOS(least(1,COS(0.390773233042196)*COS(1.9932995060120096)*COS(RADIANS(locations.lat))*COS(RADIANS(locations.lng))+
          COS(0.390773233042196)*SIN(1.9932995060120096)*COS(RADIANS(locations.lat))*SIN(RADIANS(locations.lng))+
          SIN(0.390773233042196)*SIN(RADIANS(locations.lat))))*3963.1899999999996)
          <= 50))
ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR:  missing FROM-clause entry for table "locations"
LINE 1: SELECT "merchants".* FROM "merchants" WHERE (locations.lat I...

Is there any wrong setup between model?

commented

the workaround should be Merchant.joins(:location).within(50,:origin => [lat,lng])
Stack overflow refenerce

Looking for any other solutions for this

any update for this?

I think the through only giving the methods and forget to adding the join if we look at the query

this one that using join

irb(main):002:0> Merchant.joins(:primary_address).by_distance(:origin => [37.792,-122.393])
  Merchant Load (6.3ms)  SELECT "merchants".* FROM "merchants" INNER JOIN "addresses" ON "addresses"."addressable_id" = "merchants"."id" AND "addresses"."addressable_type" = $1 AND "addresses"."primary" = $2 WHERE (addresses.lat IS NOT NULL AND addresses.lng IS NOT NULL) ORDER BY 
          (ACOS(least(1,COS(0.6595948309136971)*COS(-2.1361608313934197)*COS(RADIANS(addresses.lat))*COS(RADIANS(addresses.lng))+
          COS(0.6595948309136971)*SIN(-2.1361608313934197)*COS(RADIANS(addresses.lat))*SIN(RADIANS(addresses.lng))+
          SIN(0.6595948309136971)*SIN(RADIANS(addresses.lat))))*6376.77271)
          ASC LIMIT $3  [["addressable_type", "Merchant"], ["primary", true], ["LIMIT", 11]]

this one is not using join

irb(main):003:0> Merchant.by_distance(:origin => [37.792,-122.393])
  Merchant Load (2.5ms)  SELECT "merchants".* FROM "merchants" WHERE (addresses.lat IS NOT NULL AND addresses.lng IS NOT NULL) ORDER BY 
          (ACOS(least(1,COS(0.6595948309136971)*COS(-2.1361608313934197)*COS(RADIANS(addresses.lat))*COS(RADIANS(addresses.lng))+
          COS(0.6595948309136971)*SIN(-2.1361608313934197)*COS(RADIANS(addresses.lat))*SIN(RADIANS(addresses.lng))+
          SIN(0.6595948309136971)*SIN(RADIANS(addresses.lat))))*6376.77271)
          ASC LIMIT $1  [["LIMIT", 11]]