`
ywencn
  • 浏览: 86614 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

mongoID的Many_to_many

    博客分类:
  • Ruby
 
阅读更多

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 }
 
  • 大小: 4 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics