title: How to use preloaded collection in recursive methods
body: <p>I've the following self referential association:</p>
  
  <pre><code>class Action &lt; ActiveRecord::Base
    # self referential association
    has_many :action_parents
    has_many :parents, through: :action_parents
    has_many :action_children, class_name: 'ActionParent', foreign_key: 'parent_id'
    has_many :children, through: :action_children, source: :action
    …
    def should_finish
      should_start + duration
    end
  
    def should_start
      # my_start is a field in db: if there are no parents (root) it will use this field
      return my_start if parents.empty?
      parents.map(&amp;:should_finish).sort.last
    end
  end
  </code></pre>
  
  <p>My problem is the fact that <code>should_finish</code> and <code>should_start</code> are calling each other and even if I preload the parents it keeps resulting in many queries:</p>
  
  <pre><code>Action.includes(:parents).last.should_finish
  # a new query every time it checks for parents
  </code></pre>
  
  <p>Any idea on how to cache <code>actions</code> and <code>parents</code>?</p>
  
  <p><strong>EDIT</strong> - let me give some context:</p>
  
  <pre><code># actions table:        actions_parents table:
  # id | duration         task_id | parent_id
  # 1  | 5                2       | 1
  # 2  | 10               3       | 1
  # 3  | 20               4       | 2
  # 4  | 15               4       | 3
  #
  #                      |--------------|
  #                      | action 2     |
  #         |---------- &gt;| duration: 10 |
  #         |            |--------------|
  #         |                     |
  #  |--------------|             |---------&gt;|--------------|
  #  | action 1     |                        | action 4     |
  #  | duration: 5  |                        | duration: 15 |
  #  |--------------|             |---------&gt;|--------------|
  #         |                     |
  #         |            |--------------|
  #         |-----------&gt;| action 3     |
  #                      | duration: 20 |
  #                      |--------------|
  </code></pre>
  
  <p>PS: there are no circular dependencies.</p>
  
  <p>Assuming I have a tree <code>my_start</code> field of <code>some day at 10:00:00</code>:</p>
  
  <pre><code># action | should_start | should_finish
  # -------------------------------------
  # 1      | 10:00:00*    | 10:00:05
  # 2      | 10:00:05     | 10:00:15
  # 3      | 10:00:05     | 10:00:25
  # 4      | 10:00:25**   | 10:00:40
  #
  # * value from db since there is no parent
  # ** should_finish of parent with latest should_finish (action 3)
  </code></pre>
  
  <p>I thought it could preload all actions using <code>Action.includes(:parents)</code> </p>