如何在多个模块上使用相同的类名?

时间:2019-04-14 18:51:30

标签: ruby-on-rails ruby parent-child

如何在多个模块上使用相同的类名?

我有一个主要的ApplicationService

# /app/services/application_service.rb

class ApplicationService; end
# /app/services/processing/queue/application_service.rb

module Processing
  module Queue
    class ApplicationService < ApplicationService; end
  end
end
# /app/services/processing/callback/application_service.rb

module Processing
  module Callback
    class ApplicationService < ApplicationService; end
  end
end

如何使Rails不被混淆以及如何使用/app/services/application_service.rb

我在/app/services/processing/queue/中的所有服务都以/app/services/processing/queue/application_service.rb作为父类。

1 个答案:

答案 0 :(得分:3)

使用FQN(全名)::ApplicationService来引用您的顶级课程。

module Processing
  module Callback
    class ApplicationService < ::ApplicationService; end
  end
end