-
Type: Bug
-
Resolution: Fixed
-
Priority: Unknown
-
Affects Version/s: 8.0.3
-
Component/s: None
-
None
mongo (2.18.2)
mongoid (8.0.3)
Rails 7.04
When I rake db:mongoid:create_indexes, indexes are not created. I used tail -f log/development.rb to see index creation activity.
It seem the problem is that I put Mongoid documents in app/models/documents:
app/models/documents/signal_notification.rb
app/models/documents/bar.rb
When creating indexes, Mongoid iterates over the Mongoid::Config#models.
I have autoload_paths configured in application.rb:
module MyApp class Application < Rails::Application # Initialize configuration defaults for originally generated Rails version. config.load_defaults 7.0 config.autoload_paths += Dir[Rails.root.join('app', 'models', 'documents')]
I found this behavior in the Rails console:
~/projects/trading-app-2> rails c Loading development environment (Rails 7.0.4) irb(main):001:0> Mongoid.models => [Mongoid::GlobalDiscriminatorKeyAssignment::InvalidFieldHost, SignalNotification] irb(main):003:0> Rails.application.eager_load! => nil irb(main):004:0> Mongoid.models => [Mongoid::GlobalDiscriminatorKeyAssignment::InvalidFieldHost, SignalNotification, Bar, ...]
After loading all Rails models, Mongoid#models returns the expected Mongoid models.
I found that Mongoid#load_models does not use config.autoload_paths.
Mongoid#load seems to assume that all Mongoid models are in app/models (line 17):
15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
|
https://www.mongodb.com/docs/mongoid/master/api/Rails/Mongoid.html#load_models-instance_method
So, may need to use config.autoload_paths to find all Mongoid models?