views:

144

answers:

1

For general knowledge and entertainment purposes I am trying to add some behavoiur to Rails. What I am looking for is simply to run a Mysql "EXPLAIN" statement before every select statement that Rails runs. I think this should work ok but I am getting the error:

/usr/lib/ruby/gems/1.8/gems/activesupport-2.1.0/lib/active_support/core_ext/module/aliasing.rb:32:in alias_method': undefined methodselect_with_explain' for class `ActiveRecord::ConnectionAdapters::MysqlAdapter' (NameError)

This class is located in the initializers dir. Here is the code:

    module Explanifier

      def self.included(base)

        base.class_eval do
          extend ClassMethods
          alias_method_chain :select, :explain


        end
      end
      module ClassMethods
        def select_with_explain(sql, name = nil)
          puts "testing!!!"
          execute('EXPLAIN ' + sql, name)
          select_without_explain(sql, name)
        end
      end

    end



    class ActiveRecord::ConnectionAdapters::MysqlAdapter
      include Explanifier
    end

Can someone explain what I am missing here?

A: 

put your alias_method_chain in your ClassMethods module. because you define the method like classMethod and alias a InstanceMethod


    module Explanifier

      def self.included(base)

        base.class_eval do
          extend ClassMethods



        end
      end
      module ClassMethods
        def select_with_explain(sql, name = nil)
          puts "testing!!!"
          execute('EXPLAIN ' + sql, name)
          select_without_explain(sql, name)
        end
        alias_method_chain :select, :explain
      end

    end
shingara