类方法加载时的Rails API奇怪行为

问题描述

项目设置: 我正在运行与Mongoid链接到MongoDb 4.4.1的Rails 6.0.3.4/Ruby 7.1.2 API,并获得了一个控制器,如:

  module Api
    module V1
      class ApplicationController < ActionController::Base
        include Concerns::Api::Structuralizers::Collectionable
        include Concerns::Api::Structuralizers::Instanceable
        include Concerns::Api::Structuralizers::Showable
        include Concerns::Api::Structuralizers::Mutable
        include Concerns::Api::Structuralizers::Creatable
        include Concerns::Api::Structuralizers::Updatable
        include Concerns::Api::Structuralizers::Destroyable

        before_action :set_instance,only: %i[show edit update destroy]
        skip_before_action :verify_authenticity_token

        @@model_module = nil

        def initialize
          super
          @model_name = (@@model_module ? "#{@@model_module}::" : '') + controller_name.classify
          begin
            if @model_name && @model_name != 'Application'
              @model = @model_name.constantize
              @serializer = "#{@model_name}Serializer".constantize
            end
          rescue NameError => e
            Rails.logger.warn "TEST was unable to process model or serializer #{e}"
          end
          @acceptable_params = @regex_params = @regex_default_params = @regex_params = @valid_operators = []
        end
    
        class << self
          def model_module(model_module_name = nil)
            return @@model_module if !model_module_name

            @@model_module = model_module_name
          end
        end
      end
    end

然后这个继承行为的人:

module Api
  module V1
    class RunesController < ApplicationController
      model_module 'LanguageConcepts'
      include Concerns::Api::CommonControls
    end
  end
end

关注的是基本的CRUD集成。

我得到这种非常奇怪和令人沮丧的行为:

启动时:

rails

当我在索引路径上调用GET方法时,会得到:

Api :: V1 :: RunesController:Class的未定义方法“ model_module”

尽管使用控制台

导轨c

Api :: V1 :: RunesController.model_module

我明白了:

“语言概念”

然后,如果我将方法类声明更改为:

def self.model_module[...]

并保存,大多数情况下问题会自行解决。 然后过了一会儿(我可以说是随机的时间),它又回到了被窃听的状态,我需要再次编辑ApplicationController,将其恢复到之前的状态。 当它再次发生时,我需要向后做同样的事情,恶作剧...

我真的不知道发生了什么事。有人知道吗?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)