Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A copy of MyModel has been removed from the module tree but is still active

Whenever I change any code (except views and perhaps some controllers) I get:

A copy of MyModel has been removed from the module tree but is still active!

and I have to restart my dev server.

I have combed the interwebs on this issue, and the most commonly stated root cause is calling MyModel.some_method from some class that is not autoloaded. I have classes in lib that call like MyModel.find and MyModel.some_scope; however, I have configured Rails to autoload every file in the lib tree. I went so far as to configure autoloading of everything, including every file anywhere under app and even config.

One fact that might be relevant is that MyModel is the base of an STI hierarchy. It has two children, and one those has a child.

This is driving me nuts, because I have no more ideas for how to troubleshoot it. Meanwhile, productivity has gone to hell.

How can I find the source of this?

like image 498
Tim Scott Avatar asked Jan 31 '15 00:01

Tim Scott


2 Answers

It means your app still has references to the old version. I think it should be visible from the stack trace exactly where it is getting referenced.

Also, don't add lib to autoload, if it was meant to be autoloaded it would be. I don't know what this model is but possibly you should put it into app/services or app/models (no matter if it's ActiveRecord or not).

like image 148
mrbrdo Avatar answered Nov 08 '22 08:11

mrbrdo


I ran into the same issue. Basically, it is caused by old references when Rails is trying to reload, most likely happen to nested modules/classes like below:

module A
  def self.b
    B
  end
end

class C
  def d
    @d ||= D.new
  end
end

You need to update it to:

module A
  def self.b
    self::B # or A::B
  end
end

class C
  def d
    @d ||= C::D.new
  end
end

So in your case, every module/class NestedModuleOrClass under MyModel will need to update to MyModel::NestedModuleOrClass.

Try this and let me know if it works for you or not.

like image 2
Tian Chen Avatar answered Nov 08 '22 06:11

Tian Chen