views:

57

answers:

2

This is dragging at my nerves!

Scenario:

I am using the geocoder plugin: http://github.com/alexreisner/geocoder/
I have two models: Address and Item
the latitude and the longitude are in Address and an Item belongs_to :address

Now, when I try to do a near search:
Item.near(Person.first.address.coordinates, 20)
on an Item it fails with the following SQL:
Unknown column 'latitude' in 'field list':

SELECT *, 3956 * 2 * ASIN(SQRT(POWER(SIN((51.3883 - latitude) * PI() / 180 / 2), 2) + COS(51.3883 * PI()/180) * COS(latitude * PI() / 180) * POWER(SIN((6.65326 - longitude) * PI() / 180 / 2), 2) )) AS distance FROM `items` WHERE ((latitude BETWEEN 51.0984449275362 AND 51.6781550724638 AND longitude BETWEEN 6.188777824785 AND 7.117742175215) AND (`items`.`accepted` IS NULL AND `items`.`taken_by` IS NULL)) ORDER BY distance ASC

Don't look at the weird Math stuff in the query,
the problem lies within the FROM and the WHERE clause because
the latitude and the longitude attributes are part of the address table,
so a simple addition to the Query would solve the problem:

... FROM items, addresses WHERE ...
... null)) AND items.address_id = addresses.id ORDER BY ...

I just want to add a table to the FROM field and a condition :(
I tried using several named_scopes using :include and :joins, but none worked!
Does anybody know a solution to this? How could I insert this into a scoped ActiveRecord object?

+1  A: 

So the Item has address(es) and that's where the longitude and latitude actually exist, right? That's where I think the query is going wrong for you. You could try adding a :joins=>:address ... but I like the following better:

Since all the math occurs in the database, I think you could do:

Address.near(Person.first.address.coordinates, 20)

and then get the items that the addresses belong to, maybe even an :include=>:item or something.

Jesse Wolgamott
@Jesse yes, thanks for the fast reply, but I need to do some other named_scopes on the Item model afterwards.Also this query returns all the addresses of all the people who are near Person.first, but I want all Items which are in the near of Person.first
Flov
A: 

Did you ever find a solution to this?

Jim