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

db:create_indexes error

    • Type: Icon: Bug Bug
    • Resolution: Fixed
    • Priority: Icon: Major - P3 Major - P3
    • 9.1.0, 9.0.4
    • Affects Version/s: 9.0.0
    • Component/s: None
    • None
    • Fully Compatible
    • Ruby Drivers
    • Not Needed

      When upgrading our mongoid version to 9.0 we started hitting errors when running bin/rails db:create_indexes

      I have re-created the issue I am seeing with a fresh rails 8.0 app here on github to hopefully better illustrate the issue.

       

      bin/rails aborted!
      NameError: uninitialized constant User::DisplayName (NameError)
      
        include DisplayName
                ^^^^^^^^^^^
      /workspaces/mongoid-model-load-bug/app/models/user.rb:2:in `<class:User>'
      /workspaces/mongoid-model-load-bug/app/models/user.rb:1:in `<main>'
      /workspaces/mongoid-model-load-bug/app/models/concerns/user/display_name.rb:1:in `<main>'
      Tasks: TOP => db:create_indexes => db:mongoid:create_indexes => db:mongoid:load_models
      (See full trace by running task with --trace)
      

      DB indexing in the 9.0 release changed the loading behavior. It is doing a .sort on the files which causes issues with loading when a model has a concern loaded before the model itself.

      # https://github.com/jamis/mongoid/blob/master/lib/mongoid/loadable.rb
      def load_models(paths = model_paths)
        paths.each do |path|
          if preload_models.resizable?
            files = preload_models.map \{ |model| "#{path}/#\{model.underscore}.rb" }
          else
            files = Dir.glob("#\{path}/**/*.rb")
          end
      
          files.sort.each do |file|
            load_model(file.gsub(/^#\{path}\// , "").gsub(/\.rb$/, ""))
          end
        end
      end
      

      In the repo if you pulled it down and run inside the devcontainer that is created, bin/rails db:create_indexes, it will fail on NameError: uninitialized constant User::DisplayName (NameError)

            Assignee:
            jamis.buck@mongodb.com Jamis Buck
            Reporter:
            dustin.fisher@companycam.com Dustin Fisher
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: