Skip to content

Instantly share code, notes, and snippets.

@lorenzosinisi
Last active August 29, 2015 14:22
Show Gist options
  • Save lorenzosinisi/c2c512c50d05e1df40f6 to your computer and use it in GitHub Desktop.
Save lorenzosinisi/c2c512c50d05e1df40f6 to your computer and use it in GitHub Desktop.
Show posts nearby with Rails Model
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