Search code examples
ruby-on-railsmany-to-manymongoid

How to implement mongoid many-to-many associations?


I want to port a social network to Mongoid. The join table between friends is very large. Is there any way for Mongoid to handle this join table out of the box? I've seen a couple of in-model roll-your-own solutions to it, but nothing the looks efficient. Is there a way to handle this? Or is this a case where I shouldn't be using Mongoid?


Solution

  • this method is deprecated. you can now use references_and_referenced_in_many like so:

    class Person
      include Mongoid::Document
      field :name
      references_and referenced_in_many :preferences
    end
    
    class Preference
      include Mongoid::Document
      field :name
      references_and referenced_in_many :people
    end