Last active
August 29, 2015 14:22
-
-
Save lorenzosinisi/c2c512c50d05e1df40f6 to your computer and use it in GitHub Desktop.
Show posts nearby with Rails Model
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def self.close_by(lat1, lon1) | |
posts_around = [] | |
Post.where("latitude IS NOT NULL AND longitude IS NOT NULL").order('id DESC').each do |post| | |
distance = Post.new.distance(lat1.to_f, lon1.to_f, post.latitude.to_f, post.longitude.to_f) | |
radius = Setting.find(1).usersight | |
if (distance <= radius.to_f) | |
post_array = {} | |
if post.user.nil? | |
username = "" | |
else | |
username = post.user.username | |
end | |
post_array.merge!(:latitude => post.latitude, | |
:longitude => post.longitude, | |
:id => post.id, | |
:body => post.body, | |
:username => username, | |
:user_id => post.user_id, | |
:created_at => post.created_at, | |
:address => post.address) | |
posts_around.push(post_array) | |
end | |
end | |
posts_around | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment