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
类点