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

Association namespace scoping

    • Type: Icon: Task Task
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • 5.0.2
    • Affects Version/s: None
    • Component/s: None
    • Labels:
      None

      I reopend this https://github.com/mongoid/mongoid/issues/1207 because i think there is need to rediscuss this. Is easer to to migrate from AR to Mongoid if there some equals, so this is my reason to talk about about this.

      ActiveRecord have association scoping http://guides.rubyonrails.org/association_basics.html#controlling-association-scope and is very smart.

      when you have something like this:

      module Access
        class Website
          has_many :users, inverse_of: :website
          has_many :posts, inverse_of: :account
        end
      
        class User
          belongs_to :website, inverse_of: :users
        end
      end
      
      class Post
        belongs_to :website, inverse_of: posts, class_name: 'Access::Website'
      end
      

      And this works because module scoping traverse from down to up. So association within module it search for another associations. Finds what's exists and go up. Another association are found on top of scoping (i thinks it is App models directory).

      No need to defined class_names is more clear (for me definitely), because is able to define namespacing blocks of app, and if you communicate within module you know that are associations without class names.

      This also works with multiple nested namespace

      module Website
        module Access
          class Account
            has_many :users, inverse_of: :account
          end
        end
          
        class User
          belongs_to :account, inverse_of: :users, class_name: 'Access::Account'
        end
      end
      

      R.

            Assignee:
            Unassigned Unassigned
            Reporter:
            radeno radeno
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: