title: Caching ActiveRecord model instance methods
body: <p>Say I have a user model.  It has an instance method called status.  Status is not an association.  It doesn't follow any active record pattern because it's a database already in production.</p>
  
  <pre><code>class User &lt; ActiveRecord::Base  
  
    def status
      Connection.where(machine_user_id: self.id).last
    end    
  
  end
  </code></pre>
  
  <p>So I do this.</p>
  
  <pre><code>@users = User.all
  </code></pre>
  
  <p>First of all I can't eager load the status method.</p>
  
  <pre><code>@users.includes(:status).load
  </code></pre>
  
  <p>Second of all I can't cache that method within the array of users.</p>
  
  <pre><code>Rails.cache.write("user", @users)
  </code></pre>
  
  <p>The status method never gets called until the view layer it seems like.</p>
  
  <p>What is the recommended way of caching this method.</p>
  
  <p>Maybe this instance method is not what I want to do.  I've looked at scope but it doesn't look like what I want to do.</p>
  
  <p>Maybe I just need an association?  Then I get the includes and I can cache.</p>
  
  <p>But can associations handle complex logic.  In this case the instance method is a simple query.  What if I have complex logic in that instance method?</p>
  
  <p>Thanks for any help.</p>