title: Rails::Railtie: Trouble creating a Rails 3 gem
body: <p>I could really use another set of eyes on this so I thought I would post it here.
  A while ago I wrote a basic ActiveRecord Extension for my own educational purposes. I've been reading about Railties lately and thought I would try to get it working with Rails 3. I thought I would package it up as a gem to get a sense of that process as well.
  If I skip the Railtie and just do this as a traditional monkeypatch in the initializers folder it works fine. Using a Railtie... nothing.</p>
  
  <p>From the looks of it my Railtie is never executed and therefore nothing else seems to be happening. </p>
  
  <p>Does anyone see anything wrong here?</p>
  
  <p>Any suggestions for best practices or improvements are also welcome.</p>
  
  <p>project Gemfile:</p>
  
  <pre><code>gem 'sql_explain', :path =&gt; "/home/mike/projects/sql_explain/"
  </code></pre>
  
  <p>gemspec:</p>
  
  <pre><code>...
    spec.files = %w(README.rdoc sql_explain.rb lib/sql_explain.rb lib/railtie.rb sql_explain.gemspec)
  ...
  </code></pre>
  
  <p>sql_explain.rb</p>
  
  <pre><code>require 'lib/railtie.rb'
  </code></pre>
  
  <p>railtie.rb</p>
  
  <pre><code>require 'active_record'
  require 'sql_explain'
  
  module SqlExplain
    class Railtie &lt; Rails::Railtie
      railtie_name :sql_explain
      initializer 'sql_explain.extend.activerecord' do
        if defined?(ActiveRecord)
          ActiveRecord::ConnectionAdapters::MysqlAdapter.include SqlExplain::AR
        end
      end
    end
  end
  </code></pre>
  
  <p>sql_explain.rb</p>
  
  <pre><code>module SqlExplain
    module AR
      def self.included(base_klass)
        base_klass.send :alias_method_chain, :select, :explain
      end
  
  
      def select_with_explain(sql, name = nil)
        @connection.query_with_result = true
        result = execute('explain ' + sql, :skip_logging)
        rows = []
        result.each_hash { |row| rows &lt;&lt; row }
        result.free
        @connection.more_results &amp;&amp; @connection.next_result    # invoking stored procedures with CLIENT_MULTI_RESULTS requires this to tidy up else connection will be dropped
        exp_string = ""
        rows.each{|row| row.each_pair{|k,v| exp_string += " #{k}: #{v} |"}}
        log(exp_string, "Explanation") {}
        select_without_explain(sql, name)
      end
    end
  end
  </code></pre>