# Rails Join problem

**URL:** <https://www.ruby-forum.com/t/rails-join-problem/154316>\
**Category:** Rails\
**Created:** [November 27, 2008, 4:43pm UTC](https://www.ruby-forum.com/t/rails-join-problem/154316 "2008-11-27T16:43:57Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![VPVPVP](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/7ab992/32.png) [@VPVPVP](https://www.ruby-forum.com/u/VPVPVP)\
**Post date:** [November 27, 2008, 4:43pm UTC](https://www.ruby-forum.com/t/rails-join-problem/154316/1 "2008-11-27T16:43:57Z")

</div>

Hi, i’m trying to use the GeoKit plugin to calculate the distance  
between 2 points. So the idea is, i do a search for an article, and  
the results i want to order by distance. So i have a form where I  
enter the article (that im looking for) and my address. Then rails  
must find all articles that match with my query and order by address.

So now, i have two models: Article and User. Articles belongs\_to User  
and User has\_many Articles. At the User model i have the info related  
with my latitude and longitude.

So my Article object has three fields: id name user\_id (FK to User  
model)

And my user model has four fields id name lat (latitude) lng  
(longitude)

OK, to have access to the user info thru articles i do the query:

@articles = Article.find(:all,:conditions=\>“vectors @@ to\_tsquery  
(‘büch’)”,:joins=\>" INNER JOIN users ON users.id =  
articles.user\_id",:include=\>:user,:origin=\>“Augustusplatz,  
8,leipzig,germany”)

it works. But when i wanna add an :order=\>‘distance ASC’ it fails  
because the order by query is using a Article.lat, and Article.lng  
fields to calculate the distance, but these fields lat and lng, are  
User object’s members and not Article member.

BTW if I get the query generated by rails and i change the order by  
clause where uses articles.lat/lng to users.lat/lng it works.
