通过查询最新状态更新位置来查找特定位置的用户

发布于 2024-10-22 01:22:04 字数 1897 浏览 1 评论 0原文

我正在开发一个 Rails 应用程序,它与 Twitter 非常相似,用于通过称为“ping”的状态更新来跟踪团队成员及其更新的状态。 Twitter 将这些状态称为“推文”。

该应用程序的要点是:

Employee (:first_name, :last_name)
Ping (:datetime, :status, :latitude, :longitude)

员工模型:

class Employee < ActiveRecord::Base
  has_many :pings
  has_one  :ping, :order => "created_at DESC" # Returns the lastest Ping (employee.ping)
end

Ping 模型:

class Ping < ActiveRecord::Base
  belongs_to :employee
  acts_as_mappable  :default_units => :miles,
                    :default_formula => :sphere,
                    :distance_field_name => :distance,
                    :lat_column_name => :latitude,
                    :lng_column_name => :longitude
end

我需要按当前位置查询所有员工的最新 ping。问题是我不知道该怎么做。

如果我搜索当前位置的所有 ping,我会得到属于某个员工的多个 ping。然后,我必须将每个 ping.idemployee.ping.id 进行比较,看看其中之一是否是该员工的最新 ping。

我无法按员工搜索,因为地理位置信息位于 Ping 对象中。我唯一关心的是最新的 ping。

Ping 控制器

  def location
    pings = Ping.geo_scope(:within => params[:distance], :origin => [params[:latitude], params[:longitude]])
    render :json => pings, :include => :employee, :only => [:id, :first_name, :last_name, :status, :longitude, :latitude]
    # this returns all Pings that were ever created in this location.
  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]

I'm working on a Rails application, which is very similar to Twitter, that is used to track members of a teams and their updated status through status updates called 'pings'. Twitter calls these statuses 'tweets'.

The gist of the application is this:

Employee (:first_name, :last_name)
Ping (:datetime, :status, :latitude, :longitude)

Employee Model:

class Employee < ActiveRecord::Base
  has_many :pings
  has_one  :ping, :order => "created_at DESC" # Returns the lastest Ping (employee.ping)
end

Ping Model:

class Ping < ActiveRecord::Base
  belongs_to :employee
  acts_as_mappable  :default_units => :miles,
                    :default_formula => :sphere,
                    :distance_field_name => :distance,
                    :lat_column_name => :latitude,
                    :lng_column_name => :longitude
end

I need to query all of the employees' latest ping by the current location. The problem is I don't know how to do that.

If I search for all pings in the current location I get multiple pings that belong to an employee. I would then have to compare each ping.id with employee.ping.id to see if one of them is the LATEST ping of the employee.

I can't search by Employee because the geo location information is located in the Ping object. And the only pings I care about are the latest ones.

Ping Controller

  def location
    pings = Ping.geo_scope(:within => params[:distance], :origin => [params[:latitude], params[:longitude]])
    render :json => pings, :include => :employee, :only => [:id, :first_name, :last_name, :status, :longitude, :latitude]
    # this returns all Pings that were ever created in this location.
  end

Thanks for any feedback and help!

Thanks, Robin for the help. You inspired me to come up with the following:

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]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

终陌 2024-10-29 01:22:04

这是未经测试的,但我的建议是使用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

可能需要一些调整才能返回每个员工所需的确切数据,但原则上应该有效。

罗宾

This is untested, but my suggestion would be to use Rails' group_by method so you could group all the pings by employee_id (sorted by created at) and then iterate over the collection, returning the key (the employee_id) and the first value in the array (the most recent ping for that employee).

hash = Hash.new
pings.group_by(&:employee_id).order('created_at DESC').each do |k,v|
  hash[k] = v
end
render :json => hash

Might need some tweaking to return the exact data you need in respect of each employee but should work in principle.

Robin

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文