Module: ActsAsKing::InstanceMethods
- Defined in:
- lib/acts_as_king.rb
Instance Method Summary collapse
-
#ancestors ⇒ Object
Retrieves all of the parents of the instance.
Instance Method Details
#ancestors ⇒ Object
Retrieves all of the parents of the instance. (Think of it including grandparents as well.)
Example
If you had three records in a structure similar to this:
#<Comment id: 1, parent_id: nil>
#<Comment id: 2, parent_id: 1>
#<Comment id: 3, parent_id: 2>
You would do the following to return all ancestors
comment_3.ancestors # => [#<Comment id: 2, parent_id: 1>, #<Comment id: 1, parent_id: nil>]
68 69 70 71 72 |
# File 'lib/acts_as_king.rb', line 68 def ancestors current, all = self, [] all << current = current.parent while current.parent all end |