mongoID的Many_to_many

http://stackoverflow.com/questions/3676305/mongoid-uniqueness-validation-many-to-many-relation

 

class Person
  include Mongoid::Document
  field :name
  references_many :preferences, :stored_as => :array, :inverse_of => :people
end

class Preference
  include Mongoid::Document
  field :name
  references_many :people, :stored_as => :array, :inverse_of => :preferences
end

ps1 = Person.create(:name => 'John Doe')
pf1 = Preference.create(:name => 'Preference A')
pf2 = Preference.create(:name => 'Preference B')

ps1.preferences << pf1
ps1.preferences << pf2
ps1.save

pf1.people.each {|ps| puts ps.name }
ps1.preferences.each {|pf| puts pf.name }
 

你可能感兴趣的:(mongoid)