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 controller calls the manager
- The manager has a list of suitable Metrics, all extending from MetricBase, which has common functions for creating the actual object
- The manager calls each Metric-Class (e.g. ResourceStats)
- The Metric-Class (e.g. ResourceStats) does some calculations etc. and then calls a method in the parent class
- 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