ArgumentError: A copy of ApplicationController has been removed from the module tree but is still active

Solution 1:

For future visitors, I thought I'd provide some clarification on the original problem even though the question is pretty old and there is already an accepted answer.

The ArgumentError: A copy of X has been removed from the module tree but is still active is raised when you are trying to access an automatically reloaded class (in app directory) from one that is not automatically reloaded (in lib directory).

Solution 2:

I'm not sure exactly why this is happening, but I found a workaound. Change this:

def current_permission
  @current_permission ||= Permissions.permission_for(current_user)
end

To this:

def current_permission
  @current_permission ||= ::Permissions.permission_for(current_user)
end

The error is raised at this point in ActiveSupport:

# Load the constant named +const_name+ which is missing from +from_mod+. If
# it is not possible to load the constant into from_mod, try its parent
# module using +const_missing+.
def load_missing_constant(from_mod, const_name)
  log_call from_mod, const_name

  unless qualified_const_defined?(from_mod.name) && Inflector.constantize(from_mod.name).equal?(from_mod)
    raise ArgumentError, "A copy of #{from_mod} has been removed from the module tree but is still active!"
  end
  # ...
end

The problem only occurs when you don't fully qualify the constant name, so Rails tries looking it up in the ApplicationController namespace.