0

我正在使用 GPS 记录轨迹并将其发送到我的 Postgre DB,然后我想删除速度 = 0 的前导和尾随测量值。我这样尝试:

while track.measurements.last.speed == 0
      track.measurements.last.destroy
end

(track.measurements.first 相同)。当这段代码被执行时,一切都会停止而没有任何错误。我通过调试和一步步去发现的是,它在这两行之间反复切换:

(rdb:102) s /var/lib/gems/1.9.1/gems/activerecord-3.2.11/lib/active_record/associations.rb:156 关联=关联实例获取(名称)

(rdb:102) l
[151, 160] in /var/lib/gems/1.9.1/gems/activerecord-3.2.11/lib/active_record/associations.rb
   151      # :nodoc:
   152      attr_reader :association_cache
   153  
   154      # Returns the association instance for the given name, instantiating it if it doesn't already exist
   155      def association(name) #:nodoc:
=> 156        association = association_instance_get(name)
   157  
   158        if association.nil?
   159          reflection  = self.class.reflect_on_association(name)
   160          association = reflection.association_class.new(self, reflection)
(rdb:102) s
/var/lib/gems/1.9.1/gems/activerecord-3.2.11/lib/active_record/associations.rb:170
@association_cache[name.to_sym]

(rdb:102) l
[165, 174] in /var/lib/gems/1.9.1/gems/activerecord-3.2.11/lib/active_record/associations.rb
   165      end
   166  
   167      private
   168        # Returns the specified association instance if it responds to :loaded?, nil otherwise.
   169        def association_instance_get(name)
=> 170          @association_cache[name.to_sym]
   171        end
   172  
   173        # Set the specified association instance.
   174        def association_instance_set(name, association)

可悲的是,这对我来说毫无意义。有人可以在这里帮忙吗?

4

2 回答 2

0

我猜这track.measurements.last不是为每个循环查询,所以你的代码是一个无限循环:

while true
track.measurements.last.destroy
end

你可以做一个变体,但它不是特别有效:

last_is_zero = true
while last_is_zero 
  if track.measurements.last.speed == 0
    track.measurements.last.destroy
  else
    last_is_zero = false
  end
end
于 2013-06-17T22:00:20.393 回答
0

好吧,这似乎会干扰某些关联,我不知道为什么这不起作用,但这是我现在可以使用的解决方案:

    #remove standing time from beginning and end
    for i in 0..self.measurements.length-1
      if self.measurements[i].speed == 0
        self.measurements[i].destroy
      else
        break
      end
    end

    for i in (self.measurements.length-1).downto(0)
      if self.measurements[i].speed == 0
        self.measurements[i].destroy
      else
        break
      end
    end

感谢您的帮助!

于 2013-06-20T16:13:09.583 回答