Ruby on rails 通过查询用户的最新状态更新位置,查找特定位置的用户
我正在开发一个Rails应用程序,它与Twitter非常相似,用于通过名为“Ping”的状态更新跟踪团队成员及其更新状态。Twitter将这些状态称为“tweets” 申请的要点如下: 员工:名字,:姓 Ping:datetime,:status,:纬度,:经度 员工模式:Ruby on rails 通过查询用户的最新状态更新位置,查找特定位置的用户,ruby-on-rails,geokit,Ruby On Rails,Geokit,我正在开发一个Rails应用程序,它与Twitter非常相似,用于通过名为“Ping”的状态更新跟踪团队成员及其更新状态。Twitter将这些状态称为“tweets” 申请的要点如下: 员工:名字,:姓 Ping:datetime,:status,:纬度,:经度 员工模式: class Employee < ActiveRecord::Base has_many :pings has_one :ping, :order => "created_at DESC" # Retu
class Employee < ActiveRecord::Base
has_many :pings
has_one :ping, :order => "created_at DESC" # Returns the lastest Ping (employee.ping)
end
感谢您的反馈和帮助
谢谢你的帮助,罗宾。你激励我提出以下建议:
employees = Employee.all
current_pings = []
employees.each do |employee|
current_pings << employee.ping.id
end
pings = Ping.geo_scope(:within => params[:distance], :origin => [params[:latitude], params[:longitude]]).find_all_by_id(current_pings)
render :json => pings, :include => :employee, :only => [:id, :first_name, :last_name, :status, :longitude, :latitude, :created_at]
这是未经测试的,但我的建议是使用Rails的group_by方法,这样就可以根据在创建时排序的employee_id对所有ping进行分组,然后迭代集合,返回键employee_id和数组中的第一个值该员工最近的ping
hash = Hash.new
pings.group_by(&:employee_id).order('created_at DESC').each do |k,v|
hash[k] = v
end
render :json => hash
可能需要一些调整来返回每个员工所需的准确数据,但原则上应该可以
Robin这是未经测试的,但我的建议是使用Rails的group_by方法,这样您就可以根据在创建时排序的员工id对所有ping进行分组,然后迭代集合,返回键employee_id和数组中的第一个值该员工最近的ping
hash = Hash.new
pings.group_by(&:employee_id).order('created_at DESC').each do |k,v|
hash[k] = v
end
render :json => hash
可能需要一些调整来返回每个员工所需的准确数据,但原则上应该可以
罗宾
hash = Hash.new
pings.group_by(&:employee_id).order('created_at DESC').each do |k,v|
hash[k] = v
end
render :json => hash