Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Rails 3.2.8-如何进行递归检查?_Ruby On Rails_Ruby_Ruby On Rails 3 - Fatal编程技术网

Ruby on rails Rails 3.2.8-如何进行递归检查?

Ruby on rails Rails 3.2.8-如何进行递归检查?,ruby-on-rails,ruby,ruby-on-rails-3,Ruby On Rails,Ruby,Ruby On Rails 3,我有以下型号: class Point < ActiveRecord::Base ... has_one :next_point, class_name: "Point", foreign_key: "next_point_id" belongs_to :previous_point, class_name: "Point", foreign_key: "next_point_id" # It should return an array with the next remai

我有以下型号:

class Point < ActiveRecord::Base
 ...

 has_one :next_point, class_name: "Point", foreign_key: "next_point_id"
 belongs_to :previous_point, class_name: "Point", foreign_key: "next_point_id"

 # It should return an array with the next remaining points
 def next_remaining_points
 end

end
类点

如何执行一个返回下一个\u点直到下一个\u点为零的方法?

在Ruby代码中有一个简单的递归解决方案,但请注意,这可能会对每个调用执行DB查询。使用自定义SQL查询可能会更好

class Point < ActiveRecord::Base
  ...
  def last_point  # Or some better name
    next_point.try(:last_point) || self
  end
类点