Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

'already initialized constant' warnings when models are in additional configured auto load roots #281

Closed
bughit opened this issue Oct 5, 2015 · 8 comments
Assignees
Milestone

Comments

@bughit
Copy link

bughit commented Oct 5, 2015

to repro:
  1. git clone [email protected]:bughit/annotate_test.git
  2. rake db:migrate
how to configure additional auto load roots:

config.eager_load_paths.concat Dir["#{config.root}/app/models/additional_roots/**/"]

@bughit bughit changed the title 'already initialized constant' warinings when models are in additional configured auto load roots 'already initialized constant' warnings when models are in additional configured auto load roots Oct 5, 2015
@ctran
Copy link
Owner

ctran commented Dec 2, 2015

Can you clarify what the third-line does there?

config.eager_load_paths.concat Dir["#{config.root}/app/models/additional_roots/**/"]

@ctran ctran added the reviewed label Dec 2, 2015
@bughit
Copy link
Author

bughit commented Dec 2, 2015

modules in eager_load_paths are eager loaded in production and auto loaded in development

if you have class Foo in app/models/additional_roots/sub1/sub2/foo.rb and you refer to it somewhere Foo.new it will be autoloaded.

@ctran
Copy link
Owner

ctran commented Dec 3, 2015

I'm aware of that. I'm asking for more details in your report since it's kind of skimmed. Maybe a few more sentences would help.

@bughit
Copy link
Author

bughit commented Dec 3, 2015

  1. and 2. in the first post are exact repro directions, you should run them.

The config line is just an operational definition of "additional configured auto load roots"

@ctran
Copy link
Owner

ctran commented Dec 3, 2015

Thanks. I'll take a look

@ctran ctran self-assigned this Dec 3, 2015
@ctran ctran added the bug label Dec 3, 2015
@ctran ctran added this to the v2.7.0 milestone Dec 3, 2015
ctran added a commit that referenced this issue Dec 27, 2015
@ctran ctran closed this as completed Dec 27, 2015
@bughit
Copy link
Author

bughit commented Dec 27, 2015

have a question about the fix. I haven't debugged this, but isn't the source of the problem that some model files are being loaded twice? If so, could that not cause other problems? Would it not better to try to avoid this double loading rather than just silence the warning.

@ctran
Copy link
Owner

ctran commented Dec 28, 2015

It's tricky to know whether a model inside a file is already loaded due to
auto-loading magic, complicated further by pre-loaded paths. If you know a
better way to detect that, let me know.
On Sun, Dec 27, 2015 at 12:19 AM bughit [email protected] wrote:

have a question about the fix. I haven't debugged this, but isn't the
source of the problem that some model files are being loaded twice? If so,
could that not cause other problems? Would it not better to try to avoid
this double loading rather than just silence the warning.


Reply to this email directly or view it on GitHub
#281 (comment)
.

@ghost
Copy link

ghost commented Nov 3, 2017

@ctran I think the tricky is to use $LOAD_PATH. The code below should work:

def get_model_class(file)
  # ...
  get_loaded_model(model_path) || raise(BadModelFileError.new)
  # ...
end

def get_loaded_model_fixed(model_path, file)
  klass = get_loaded_model(model_path)
  return klass if klass

  absolute_file = File.expand_path(file)
  model_paths = $LOAD_PATH.select { |load_path| absolute_file.include?(load_path) }.map { |load_path| absolute_file.sub(load_path, '').sub(/^\//, '').sub(/\.rb$/, '') }
  model_paths.map { |model_path| get_loaded_model(model_path) }.find { |loaded_model| !loaded_model.nil? }
end

ghost pushed a commit to dao42/annotate_models that referenced this issue Nov 4, 2017
…e in additional configured auto load roots
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants