2015-05-21 17 views
0

我在中央和协调器之间有一个has_many_and_belongs_to关系。 所以我蒙戈文档如下表示:N-N mongoid中的其他属性(如在数据透视表中)

central = { 
    _id: 1, 
    title: 'First Name', 
    coordinators: [ 
     BSON[1], 
     BSON[2], 
     BSON[3] 
    ] 
} 

coordinators = [ 
    { 
     _id: 1, 
     name: 'Me', 
     centrals: [BSON[1], BSON[2]] 
    }, 
    { 
     _id: 1, 
     name: 'Mateus' 
     centrals: [BSON[1]] 
    }, 
    { 
     _id: 1, 
     name: 'Gomes' 
     centrals: [BSON[1]] 
    }, 
] 

如果我这样做:

@central = Central.find(1) 
@coordinator = @central.coordinators.find(1) 

@coordinator.can_edit = false 

这将适用于造成这种协调员文件:

coordinator = { 
    _id: 1, 
    name: 'Me', 
    centrals: [BSON[1], BSON[2]], 
    can_edit: false 
} 

但是我真正想要做的是在关系中应用这个can_edit属性,就像在数据透视表中一样ñRDBMS:

central = { 
    _id: 1, 
    titulo: 'First Name', 
    coordinators: [ 
     { 
      _id: 1, 
      name: 'Me', 
      can_edit: false 
     }, 
     BSON[2], 
     BSON[3] 
    ] 
} 

仅对中央与ID 1我想将can_edit APLY为false。

我必须保持Central和Coordinator之间的关系,但在某些情况下,我希望获得关于该关系的附加信息,例如,如果我不允许协调员仅在ID为1的中央编辑某些数据

如何使用mongoid来做到这一点?

回答

0

对此的解决方案是创建另一个关系NN:

添加上central.rb

has_and_belongs_to_many :blocked_coordenadors, 
          class_name: "Central", 
          inverse_of: :blocked_centrals 

而且在coordinator.rb

has_and_belongs_to_many :blocked_centrals, 
          class_name: "Central", 
          inverse_of: :blocked_coordenadors 

,并检查我这样做:

central.blocked_coordenadors.include? coordinator 
相关问题