-
Notifications
You must be signed in to change notification settings - Fork 645
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
Comments
Can you clarify what the third-line does there? config.eager_load_paths.concat Dir["#{config.root}/app/models/additional_roots/**/"] |
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 |
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. |
The |
Thanks. I'll take a look |
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. |
It's tricky to know whether a model inside a file is already loaded due to
|
@ctran I think the tricky is to use 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 |
…e in additional configured auto load roots
to repro:
git clone [email protected]:bughit/annotate_test.git
rake db:migrate
how to configure additional auto load roots:
config.eager_load_paths.concat Dir["#{config.root}/app/models/additional_roots/**/"]
The text was updated successfully, but these errors were encountered: