Add table_name to near scopes #179

Closed
wants to merge 2 commits into
from

Conversation

Projects
None yet
3 participants

I had an issue with a near() scopes and some gnarly custom joins, where mysql had an issue with the ambiguity of the lat/lng attributes. I didn't run the tests and I'm unfamiliar with the geocoder codebase, but I followed the pattern from other parts of the same class and it's working for my specific examples.

Actually, that first commit (1df0a41) is unnecessary and can be ignored/cherry-picked around. Sorry about that.

I had a similar issue with a pretty simple self-join:

scope :etches, lambda {
  joins('LEFT JOIN notes AS etches ON notes.id = etches.original_note_id')
    .where('etches.id IS NOT NULL').uniq     
}

Tony's fork works for me.

Owner

alexreisner commented Apr 11, 2012

This functionality has been added as a result of some other pull requests and issues. Thanks!

I just tried head, but it looks like this does not resolve the issue for me:

> Note.near([35.687, -105.938], 500).etches.all
  Note Load (0.3ms)  SELECT DISTINCT notes.*, 3958.755864232 * 2 * ASIN(SQRT(POWER(SIN((35.687 - notes.latitude) * PI() / 180 / 2), 2) + COS(35.687 * PI() / 180) * COS(notes.latitude * PI() / 180) * POWER(SIN((-105.938 - notes.longitude) * PI() / 180 / 2), 2) )) AS distance, CAST(DEGREES(ATAN2( RADIANS(longitude - -105.938), RADIANS(latitude - 35.687))) + 360 AS decimal) % 360 AS bearing FROM "notes" LEFT JOIN notes AS etches ON notes.id = etches.original_note_id WHERE "notes"."deleted_at" IS NULL AND (3958.755864232 * 2 * ASIN(SQRT(POWER(SIN((35.687 - notes.latitude) * PI() / 180 / 2), 2) + COS(35.687 * PI() / 180) * COS(notes.latitude * PI() / 180) * POWER(SIN((-105.938 - notes.longitude) * PI() / 180 / 2), 2) )) <= 500) AND (etches.id IS NOT NULL) ORDER BY distance
ActiveRecord::StatementInvalid: PG::Error: ERROR:  column reference "longitude" is ambiguous
LINE 1: ...), 2) )) AS distance, CAST(DEGREES(ATAN2( RADIANS(longitude ...
                                                             ^
Owner

alexreisner commented May 2, 2012

Oops, sorry about that. Give it another shot now?

Awesome, that works!

Owner

alexreisner commented May 2, 2012

Great. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment