使用Mongoid数据库设计的多对多关系?

时间:2022-10-04 11:56:34

i try to implement the following database structure, but have problems understanding how to do so using mongoid:

我尝试实现以下数据库结构,但在使用mongoid理解如何执行此操作时遇到问题:

I have a model Documents, a model DocumentTeam and a model Employees. Users can create documents and can choose employees, which get added to the DocumentTeam. This is where I'm atm:

我有一个模型文档,一个模型DocumentTeam和一个模型Employees。用户可以创建文档并可以选择添加到DocumentTeam的员工。这就是我的地方:

class Document
  embeds_one :document_team
end

class DocumentTeam
  has_many :employees
  embedded_in :document
end

class Employee
  belongs_to :document_teams
end

so my question: how can i tell rails to automatically insert a chosen Employee into the the embedded DocumentTeam when creating a Document?

所以我的问题是:我如何告诉rails在创建文档时自动将选定的Employee插入到嵌入式DocumentTeam中?

Furthermore I want to be able to list e.g. all briefings of a Employee by

此外,我希望能够列出例如所有员工的简报

Employee.first.documents

is this possible as well?

这有可能吗?

thanks advance!

谢谢提前!

1 个解决方案

#1


1  

In mongoid you can not reference embedded documents. You can reference root documents from embedded documents, but not the other way around. i.e. you can not have belongs_to :document_teams in Employee. Also, as a side effect is that relations in embedded documents should be single sided. You can change you modeling to following to achieve what you want:

在mongoid中,您无法引用嵌入的文档。您可以从嵌入式文档中引用根文档,但不能反过来。即你不能在Employee中拥有belongs_to:document_teams。此外,副作用是嵌入式文档中的关系应该是单面的。您可以将建模更改为以下内容以实现您的目标:

class Document
  embeds_one :document_team
end

class DocumentTeam
  has_and_belongs_to_many :employees, inverse_of: nil
  embedded_in :document
end

class Employee
  def documents
    Document.where('document_team.employee_ids' => self.id)
  end
end

This will let you use Employee.first.documents, but you can not treat it as a relation and go on doing things you can do with relations, like reassigning, pushing and pulling documents. You have to manage teams and employees relations through DocumentTeam but can access an employees documents directly for reading.

这将允许您使用Employee.first.documents,但您不能将其视为关系,并继续执行您可以对关系执行的操作,例如重新分配,推送和拉取文档。您必须通过DocumentTeam管理团队和员工关系,但可以直接访问员工文档以供阅读。

PS: Document id not a good name for class, I guess it may * with Mongoid::Document in some scenario.

PS:文档ID不是类的好名字,我猜它可能会在某种情况下与Mongoid :: Document发生冲突。

#1


1  

In mongoid you can not reference embedded documents. You can reference root documents from embedded documents, but not the other way around. i.e. you can not have belongs_to :document_teams in Employee. Also, as a side effect is that relations in embedded documents should be single sided. You can change you modeling to following to achieve what you want:

在mongoid中,您无法引用嵌入的文档。您可以从嵌入式文档中引用根文档,但不能反过来。即你不能在Employee中拥有belongs_to:document_teams。此外,副作用是嵌入式文档中的关系应该是单面的。您可以将建模更改为以下内容以实现您的目标:

class Document
  embeds_one :document_team
end

class DocumentTeam
  has_and_belongs_to_many :employees, inverse_of: nil
  embedded_in :document
end

class Employee
  def documents
    Document.where('document_team.employee_ids' => self.id)
  end
end

This will let you use Employee.first.documents, but you can not treat it as a relation and go on doing things you can do with relations, like reassigning, pushing and pulling documents. You have to manage teams and employees relations through DocumentTeam but can access an employees documents directly for reading.

这将允许您使用Employee.first.documents,但您不能将其视为关系,并继续执行您可以对关系执行的操作,例如重新分配,推送和拉取文档。您必须通过DocumentTeam管理团队和员工关系,但可以直接访问员工文档以供阅读。

PS: Document id not a good name for class, I guess it may * with Mongoid::Document in some scenario.

PS:文档ID不是类的好名字,我猜它可能会在某种情况下与Mongoid :: Document发生冲突。