1

我的第一个 ruby​​ 和 rails 应用程序有问题。在开发时我最近遇到了这个错误并且无法解决它:A copy of MetricBase has been removed from the module tree but is still active. 当我对文件或相关文件进行更改时,就会发生这种情况。通过重新启动服务器来解决它,但这很不方便。所以我会很感激任何帮助。还有关于任何错误使用铁轨的问题。

对于上下文:我的数据库中有一些我想分析的数据。为此,我想到了以下结构。

度量系统的调用结构

  1. 控制器调用管理器
  2. 管理器有一个合适的 Metrics 列表,都是从 MetricBase 扩展而来的,具有创建实际对象的通用功能
  3. 经理调用每个 Metric-Class(例如 ResourceStats)
  4. Metric-Class(例如 ResourceStats)进行一些计算等,然后调用父类中的方法
  5. 父类 (MetricBase) 创建一个 Metric-Model

此时出现错误,直接输入后make_metric_model

class MetricBase
  def initialize(id, resource)
    @id = id
    @resource = resource
    @metrics = []
  end

  def make_metric_model(name, desc, value)
    # here the error is thrown
    metric = Metric.where({ metric_id: @id, name: name }).first 

    metric = Metric.new if metric.nil?

    metric.metric_id = @id
    metric.name = name
    metric.description = desc
    metric.value = value
    metric.resource = @resource

    @metrics.push(metric)
  end

  def get_metrics
    return @metrics
  end
end

错误跟踪

ArgumentError - A copy of MetricBase has been removed from the module tree but is still active!:
  activesupport (4.0.0) lib/active_support/dependencies.rb:445:in `load_missing_constant'
  activesupport (4.0.0) lib/active_support/dependencies.rb:183:in `const_missing'
  app/models/metric_base.rb:9:in `make_metric_model'
  app/models/resource_stats.rb:11:in `analyze'
  app/models/metrics_manager.rb:20:in `block in run_all_metrics'
  app/models/metrics_manager.rb:11:in `run_all_metrics'
  app/controllers/metrics_controller.rb:12:in `run'

我曾尝试按照另一个问题中的建议添加::Metric但同样的事情发生了。请注意,我来自 C++、Java 和 PHP 的背景。

我正在使用 Rails 4 和 Ruby 2。

非常感谢任何帮助或提示!

谢谢,卢卡斯

4

1 回答 1

0

我仍然不确定最初的问题是什么。一种解决方法是使用 zeus 并在编辑后终止并重新启动服务器。

我现在使用的一个更好的解决方案是将类放在 a 中module Metrics并且它可以工作。

于 2013-10-22T12:22:51.190 回答