Circular dependency when trying to reopen an engine class?

367 Views Asked by At

I am trying to re-open a class in Rails that comes from an engine. I did the following:

module Xaaron
  ApiKey.class_eval do
     include Promiscuous::Publisher
     publish :xaaron_users_id, :api_key, :as => :ApiKey
  end
end

which sits in:

  models/
    Xaaron/
      api_key.rb

This is all in my app, which should then let me run:

bundle exec promiscuous publish "Xaaron::ApiKey.all"

But, when I do, I get the error:

RuntimeError: Circular dependency detected while autoloading constant Xaaron::ApiKey
2

There are 2 best solutions below

0
On

Like @artemave stated, rename the file. And if that does not work, try:

Xaaron::ApiKey.class_eval do
0
On

TL;DR: Try renaming api_key.rb to something else.

Here is what I think is happening: Rails autoloader loads Xaaron/api_key.rb and encounters an unknown constant Xaaron::ApiKey. It then tries to load it from file called (based on constant name)... Xaaron/api_key.rb. Boom, circular dependency.