質問

私は本当にこれに別の目を使うことができたので、ここに投稿すると思いました。しばらく前に、私は自分の教育目的で基本的なActiverCord拡張機能を書きました。私は最近Railtiesについて読んでいて、Rails 3で動作させようとすると思っていました。そのプロセスの感覚を得るために宝石としてパッケージ化すると思いました。 RailTieをスキップして、これをイニシャルアイザーフォルダーの従来のMonkeypatchとして実行すると、正常に動作します。 Railtieを使用...何もありません。

見た目から、私のRailtieは実行されることはなく、したがって他に何も起こっていないようです。

誰かがここで何か問題を見ていますか?

ベストプラクティスや改善に関する提案も歓迎します。

ProjectGemfile:

gem 'sql_explain', :path => "/home/mike/projects/sql_explain/"

GEMSPEC:

...
  spec.files = %w(README.rdoc sql_explain.rb lib/sql_explain.rb lib/railtie.rb sql_explain.gemspec)
...

sql_explain.rb

require 'lib/railtie.rb'

railtie.rb

require 'active_record'
require 'sql_explain'

module SqlExplain
  class Railtie < 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

sql_explain.rb

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 << row }
      result.free
      @connection.more_results && @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
役に立ちましたか?

解決

あなたはすでにこれを整理しているように見えますが、Rails 3でできることを忘れないでください:

ActiveSupport.on_load :active_record do
  ActiveRecord::ConnectionAdapters::MysqlAdapter.include SqlExplain::AR
end

これにより、ActivereCordがロードされたら、インクルードが解雇されることを保証します。

他のヒント

これは本当ですか?:

if defined?(ActiveRecord)

私はそれが間違っていると思います。 「Rails」の代わりに、「Rails/All」を要求しようとします - 最初のものはARのロードではありません。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top