title: Extending controllers of a Rails 3 Engine in the main app
body: <p>I am using a Rails engine as a gem in my app. The engine has <code>PostsController</code> with a number of methods and I would like to extend the controller logic in my main app, e.g. to add some methods. If I just create <code>PostsController</code> in the main app, then the engine's controller is not loaded.</p>
  
  <p>There is a solution proposed in question <a href="https://stackoverflow.com/questions/2964050/rails-engines-extending-functionality">Rails engines extending functionality</a> based on altering <code>ActiveSupport::Dependencies#require_or_load</code></p>
  
  <p>Is it the only/correct way to do this? If yes, where do I put that piece of code?</p>
  
  <p>EDIT1:</p>
  
  <p>This is the code <a href="https://stackoverflow.com/questions/2964050/rails-engines-extending-functionality/2990539#2990539">suggested by Andrius</a> for Rails 2.x</p>
  
  <pre><code>module ActiveSupport::Dependencies
    alias_method :require_or_load_without_multiple, :require_or_load
    def require_or_load(file_name, const_path = nil)
      if file_name.starts_with?(RAILS_ROOT + '/app')
        relative_name = file_name.gsub(RAILS_ROOT, '')
        @engine_paths ||= Rails::Initializer.new(Rails.configuration).plugin_loader.engines.collect {|plugin| plugin.directory }
        @engine_paths.each do |path|
          engine_file = File.join(path, relative_name)
          require_or_load_without_multiple(engine_file, const_path) if File.file?(engine_file)
        end
      end
      require_or_load_without_multiple(file_name, const_path)
    end
  end
  </code></pre>