Uploaded image for project: 'Mongoid'
  1. Mongoid
  2. MONGOID-5547

rake db:mongoid:create_indexes fails for models that are stored outside of /app/models.

    • Type: Icon: Bug Bug
    • Resolution: Fixed
    • Priority: Icon: Unknown Unknown
    • 8.1.0
    • 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
      1. File 'build/mongoid-master/lib/rails/mongoid.rb', line 15def load_models(app)app.config.paths["app/models"].expanded.each do |path|preload = ::Mongoid.preload_modelsif preload.resizable?files = preload.map { |model| "# {path}/#{model.underscore}.rb" }elsefiles = Dir.glob("#{path}

        /*/.rb")endfiles.sort.each do |file|load_model(file.gsub("#

        {path}

        /" , "").gsub(".rb", ""))endendend

      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?

       

       

            Assignee:
            jamis.buck@mongodb.com Jamis Buck
            Reporter:
            george@boost-media.com George Ulmer
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: