保存关系属性的最佳方法是什么?例如,我有模型人和项目(M:N关系)。我想保存人物在项目中的角色。在关系数据库中,它将是enction表Person_project中的一个属性。在mongoid(ror)中欺骗这一点的最佳方法是什么?
和第二个问题:我应该将ID保存在许多或仅一侧的许多关系中吗?
文档:http://mongoid.org/en/mongoid/docs/relations.html#has_and_and_belongs_to_to_many
class Person
include Mongoid::Document
has_and_belongs_to_many :projects
end
class Project
include Mongoid::Document
has_and_belongs_to_many :persons
end
mongoid使用数组在两个集合中存储ID。Person
集合具有一个字段project_ids
,Project
Collection有一个字段person_ids
我不会在项目中存储用户或特定用户角色(超级用户除外,或出于最终的缓存原因)。仅仅是因为有两个生命周期,当您在做项目工作时,您可能不会与用户在一起。因此,我一开始就将角色设置存储在这样的人中:
{
_id : Obj{},
projects : [
{project_one : role_name}, //use a name if name is unique if not use project_id as key
{project_two : role_name}
]
}
或更容易,但需要更多空间
{
_id : Obj{},
projects : [
{project_id : project_one,
role : role_name},...
]
}
即使您可以将一些与用户相关的内容存储在项目中,我也总是会针对用户测试角色。