1

I have a problem with my first ruby and rails application. When developing I have recently come across this error and not been able to solve it: A copy of MetricBase has been removed from the module tree but is still active. That happens when I make changes to the file, or a related file. It is resolved by restarting the server but that is quite inconvenient. So I would appreciate any help. Also regarding any wrong use of rails.

For context: I have some data in my database which I would like to analyze. For this I thought of the below structure.

The call structure of the Metrics System

  1. The controller calls the manager
  2. The manager has a list of suitable Metrics, all extending from MetricBase, which has common functions for creating the actual object
  3. The manager calls each Metric-Class (e.g. ResourceStats)
  4. The Metric-Class (e.g. ResourceStats) does some calculations etc. and then calls a method in the parent class
  5. The parent class (MetricBase) creates a Metric-Model

At this point the error occurs, directly after entering 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

The error trace

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'

I have tried adding :: before Metric as suggested in another question, but the same thing happens. Note that I come from a background of C++, Java and PHP.

I am using Rails 4 and Ruby 2.

Any help or tips are greatly appreciated!

Thanks, Lukas

Luksurious
  • 991
  • 9
  • 15

1 Answers1

0

I'm still not sure what the original issue is/was. A workaround was to use zeus and kill and restart the server after an edit.

A better solution I am now using is putting the classes in a module Metrics and it works.

Luksurious
  • 991
  • 9
  • 15
  • where you able to get zeus to automatically restart the server after an edit? Or did you have to do it manually yourself? – joshua.paling Mar 23 '14 at 04:26
  • Perhaps this answer might explain what you were running into, where not all files are automatically reloaded in the dev environment: http://stackoverflow.com/a/23008837/149503 – stereoscott Oct 27 '14 at 04:57