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

Document how association class names are looked up when modules are involved

    • Type: Icon: Improvement Improvement
    • Resolution: Unresolved
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: None
    • Component/s: Associations, Docs
    • Labels:
      None

      The issue of association class name lookups when modules are involved has come up several times in my time at MongoDB, most recently in https://jira.mongodb.org/browse/MONGOID-5080. Our tutorial on the association page doesn't include the word "module" at all, hence appears to be silent on the matter.

      Specifically, when models are defined in a module and they reference each other via associations, the class name must be specified on each reference where the target is in a module, even if the host is in the same module. For example, in the following snippet, both of the class_name options are required:

      module Blog
      
        class Article
      
          include Mongoid::Document
      
          embeds_many :comments, class_name: "Blog::Comment"
      
        end
      
       
      
        class Comment
      
          include Mongoid::Document
      
          embedded_in :article, class_name: "Blog::Article"
      
        end
      
      end
      

      This ticket is to:

      Verify this behavior for all 7 associations (4 referenced, 3 embedded)
      Add tests if needed
      Document it in tutorial with examples

      Note that https://jira.mongodb.org/browse/MONGOID-5080 proposes to change Mongoid's behavior to where :class_name option would be required when referencing models in non-host namespaces, which would require different documentation and tests.

            Assignee:
            Unassigned Unassigned
            Reporter:
            oleg.pudeyev@mongodb.com Oleg Pudeyev (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated: