これは一般的な Ruby の質問だと思いますが、私の場合、関連するオブジェクトは ActiveRecord モデルです。
別のモデル内にネストできるモデルがある場合、モデルのネストの深さをどのように判断できますか?
いいえ:
Model Root (Level 0)
- Model Level 1
- - Model Level 2
- - Model Level 2
- - Model Level 2
- - - Model Level 3
- - - Model Level 3
- Model Level 1
- Model Level 1
では、foo
3 レベルの深さでネストされたモデルがあるとします (上記のように)。呼び出すfoo.parent.parent.parent
と、ルート モデルが取得されます。
foo.depth
foo とそのルートの間にいくつのレベルがあるかを返すようなメソッドを定義するにはどうすればよいでしょうか?
ありがとう!