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

How to implement 'as' in Mongoid 3 with has_and_belongs_to_many

    • Type: Icon: Task Task
    • Resolution: Done
    • 3.1.4
    • Affects Version/s: None
    • Component/s: None
    • Labels:
      None

      I have just upgraded to Mongoid 3.0x in my new project and a few things have broken.

      One in particular is my has_and_belongs_to_many relationship used to have an 'as' clause but now that doesn't work.

      How should I implement models where (for example) a Band can have both musicians and fans? I tried something like this but it's not right. The docs are alas out of date in this respect.

      class Band
      include Mongoid::Document
      field :name, type: String
      has_and_belongs_to_many :musicians, as: :member_of, class_name: "User"
      has_and_belongs_to_many :fans, as: :fan_of, class_name: "User"

      end

      class User
      include Mongoid::Document
      field :name, type: String
      has_and_belongs_to_many :bands, as: :member
      has_and_belongs_to_many :bands, as: :fan

      end

      Sorry if this is a noob question, today is my first day with MongoDB and Mongoid.

            Assignee:
            durran Durran Jordan
            Reporter:
            davesag davesag
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: