I have a simple model setup in my Ruby on Rails app. (User {name, username, lat, lon}) and am writing a basic extension to the model. I would like the method to return users within a certain distance. It all works just fine in the page view, but as I am debugging I would like to work through some testing using the script/console.
My question: It seems to be printing to the screen the entire result set when I run it from the command line and script/console.
My model:
class User < ActiveRecord::Base
def distance_from(aLat, aLon)
Math.sqrt((69.1*(aLat - self.lat))**2 + (49*(aLon - self.lon))**2 )
end
def distance_from_user(aUser)
distance_from(aUser.lat, aUser.lon)
end
def users_within(distance)
close_users = []
users = User.find(:all)
users.each do |u|
close_users << u if u.distance_from_user(self) < distance
end
return close_users
end
end
and from the command line I am running
>> u = User.find_by_username("someuser")
>> print u.users_within(1)
So, I guess I would like to know why it’s printing the whole result set, and if there is a way to suppress it so as to only print what I want?
It appears that the console interface of the Ruby application evaluates each line as though it were entered from the console.
I would have expected it to some encapsulation of the scripts but it makes sense as it is being interpreted on the fly by the ruby interactive console line.
And to clarify as to the prefered sql base solution, doing find_by_sql is frowned upon in most refereces I have found. The SQL based solution that I reccomend is