Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
SunnyTam opened this issue Dec 17, 2015 · 2 comments
Open

Comments

@SunnyTam
Copy link

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?

@SunnyTam
Copy link
Author

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

Looking for any other solutions for this

@buncis
Copy link

buncis commented Sep 18, 2020

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]]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants