0

私は次のモデルを持っています:

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

next_point が nil になるまで next_point を返すメソッドを実行するにはどうすればよいですか?

4

1 に答える 1

0

There is a simple recursive solution to this in your Ruby code, but be warned that this can execute a DB query for each call. You may be better off with a custom SQL query.

class Point < ActiveRecord::Base
  ...
  def last_point  # Or some better name
    next_point.try(:last_point) || self
  end
于 2012-10-21T22:44:14.060 に答える