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

includes not taken into account #3

Open
benhutton opened this issue Nov 3, 2010 · 7 comments
Open

includes not taken into account #3

benhutton opened this issue Nov 3, 2010 · 7 comments
Labels

Comments

@benhutton
Copy link

I have a model, User, with the following line:
acts_as_mappable :through => { :profile => :address }

I expected the following to work:
User.in_bounds(bounds).all

It doesn't. I get error messages like Unknown column 'addresses.lat' in 'where clause'. I need to do the following:
User.includes(:profile => :address).in_bounds(bounds).all

...and everything will work.

@mcmire
Copy link

mcmire commented Nov 15, 2010

I can confirm this. My setup:

  • geokit-rails 0.1.2
  • Ruby 1.9.2
  • Database: Postgres
  • Models
    • Location (acts_as_mappable)
    • PairingSession (belongs_to :location, acts_as_mappable :through => :location)

Saying PairingSession.geo_scope(:within => 10, :origin => some_origin) produced this query:

SELECT *, 
 (ACOS(least(1,COS(0.699016602649681)*COS(-1.8361509014984057)*COS(RADIANS(locations.lat))*COS(RADIANS(locations.lng))+
 COS(0.699016602649681)*SIN(-1.8361509014984057)*COS(RADIANS(locations.lat))*SIN(RADIANS(locations.lng))+
 SIN(0.699016602649681)*SIN(RADIANS(locations.lat))))*3963.19)
 AS distance FROM "pairing_sessions" WHERE (locations.lat>39.90613128854641 AND locations.lat<40.19527099425359 AND locations.lng>-105.39255977003717 AND locations.lng<-105.01483463996283) AND (
 (ACOS(least(1,COS(0.699016602649681)*COS(-1.8361509014984057)*COS(RADIANS(locations.lat))*COS(RADIANS(locations.lng))+
 COS(0.699016602649681)*SIN(-1.8361509014984057)*COS(RADIANS(locations.lat))*SIN(RADIANS(locations.lng))+
 SIN(0.699016602649681)*SIN(RADIANS(locations.lat))))*3963.19)
 <= 10)

which failed with:

PGError: ERROR:  missing FROM-clause entry for table "locations"
LINE 2: ...16602649681)*COS(-1.8361509014984057)*COS(RADIANS(locations....
                                                             ^

Adding a .includes(:location) to the end of the query correctly added this to the query:

FROM "pairing_sessions" LEFT OUTER JOIN "locations" ON "locations"."id" = "pairing_sessions"."location_id" WHERE ...

@jlecour
Copy link
Contributor

jlecour commented Nov 16, 2010

@mcmire

It is definitely possible that making a Model mappable through another one fails.

I know that I've tried to make this work yet. I've just copied the original code.
Unfortunately I don't have the time to do this in the next few weeks/months.

I don't think that you should need to add an include scope in the query chain, geokit-rails3 should do this for you when it notices that you use a :through option. But as I've just said, I'm pretty sure it is not working right now.

If it is something that is important to you, you can fork the repository and try to make this work. I'd be happy to merge a pull request from you (or anyone else) if it has the corresponding tests.

@mcmire
Copy link

mcmire commented Nov 17, 2010

Cool. I'll do some investigation and let you know.

@cbartlett
Copy link

I can confirm this same issue. Your workaround works, thanks.

I patched it right in the model by overriding the within method like so:

def self.within(*args)
  super(*args).includes(NAME_OF_ASSOCIATION)
end

@charliegodsoe
Copy link

Big thanks for that workaround :)

@mcmire
Copy link

mcmire commented Jan 30, 2011

I have a fix for this in #8.

@novemberkilo
Copy link

Thanks @cbartlett - using your workaround

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

No branches or pull requests

6 participants