title: Re-open an ActiveRecord model that's provided by a gem
body: <p>I'm trying to extend an ActiveRecord model (<code>Vote</code>) that a gem (<a href="https://github.com/peteonrails/vote_fu" rel="noreferrer">https://github.com/peteonrails/vote_fu</a>) provides to my application. (I.e., there is no <code>vote.rb</code> in <code>app/models</code>)</p>
  
  <p>My first approach was to create a file called <code>lib/extend_vote.rb</code> that contains the code:</p>
  
  <pre><code>Vote.class_eval do
    after_create :create_activity_stream_event
    has_one :activity_stream_event
  
    def create_activity_stream_event
      # something..
    end
  end
  </code></pre>
  
  <p>This works when the first vote is created, but when I try to create each subsequent vote I get the error <code>TypeError (can't dup NilClass)</code>.</p>
  
  <p>I think this error is caused by the fact that the <code>Vote</code> class is reloaded automatically after every request, but the code in <code>lib/extend_vote.rb</code> is loaded only once when  the server starts and this causes the <code>has_one :activity_stream_event</code> association to behave weirdly. (Also, the problem goes away if I set <code>config.cache_classes = true</code> in <code>development.rb</code>)</p>
  
  <p>To solve this problem, I tried to make the vote extensions reload on every request by adding a <code>to_prepare</code> block to my <code>development.rb</code>:</p>
  
  <pre><code>config.to_prepare do
    load 'extend_vote.rb'
  end
  </code></pre>
  
  <p>This solves the <code>(can't dup NilClass)</code> problem, but now whenever I create a new vote, the <code>create_activity_stream_event</code> callback gets called an additional time. I.e., the first vote calls it once, the second calls it twice, etc, etc. So it seems like the <code>to_prepare</code> block is reloading the extension TOO aggressively and adding duplicate callbacks.</p>
  
  <p>What's the best way to add methods and callbacks to this <code>Vote</code> model?</p>