简体   繁体   English

Rails ActiveRecord关联

[英]Rails activerecord associations

I apologize for a beginner question, but I'm trying to wrap my head around ActiveRecord associations for the project I'm trying to start. 对于一个初学者的问题,我深表歉意,但是我想将自己想开始的项目的ActiveRecord关联起来。

I'm beginning to expand my knowledge in Rails and I'm a bit confused with the available ActiveRecord associations, that is which one should I pick and how to structure the models. 我开始扩展我在Rails方面的知识,并对可用的ActiveRecord关联有点困惑,那是我应该选择哪种关联以及如何构建模型。

This is the general concept: 这是一般概念:

  • You would first define a Company - that is the first part of the tree 您首先要定义一个公司-这是树的第一部分
  • Secondly, you need to define the Departments. 其次,您需要定义部门。 They should belong to the Company in question, and have a simple name and description. 它们应属于相关公司,并具有简单的名称和描述。
  • And finally - Employees - each employee can be in one Department and fall under one Company. 最后-员工-每个员工可以在一个部门中,并隶属于一个公司。

What I'm looking is the best way to create the models for Company, Departments and Employees, so I can assign Employees to the correct Company and Department. 我正在寻找的是为公司,部门和员工创建模型的最佳方法,因此我可以将员工分配给正确的公司和部门。

If you could point me to the right direction, book or tutorial/article to ease up these database table joins in my head and pick the right route for the project! 如果您能为我指明正确的方向,请本书或教程/文章来缓解这些数据库表连接的麻烦,并为项目选择正确的路线!

Any help is appreciated! 任何帮助表示赞赏!

Seems straight forward. 似乎直截了当。

Your Company model should show the following: 您的公司模型应显示以下内容:

has_many :departments
has_many :employees

One could argue you could change the employees to a "has_many :employees, through: :departments". 有人可能会说您可以将雇员更改为“ has_many:employees,via::departments”。 I don't suspect it matters too greatly. 我不认为这太重要了。

Your Department model should have: 您的部门模型应具有:

belongs_to :company
has_many :employees

Your Employee model should have: 您的员工模型应具有:

belongs_to :company
belongs_to :department

You don't technically need the company line if you use the through departments for that relationship. 如果您将直通部门用于该关系,则从技术上讲,您不需要公司电话。 But if you do this you can then run things like: 但是,如果执行此操作,则可以运行以下内容:

Employee.first.company Employee.first.department Employee.first.company Employee.first.department

Department.first.business Department.first.employees.first (notice this is plural as it's has_many) Department.first.business Department.first.employees.first(请注意,这是复数形式,因为它具有has_many)

Company.first.employees.first (will work if you have a through or if you assign it directly) Company.first.departments.first Company.first.employees.first(如果您有直通或直接分配,则可以使用)Company.first.departments.first

A "belongs_to" relationship means the model declaring it holds a foreign key for the model it belongs to. “ belongs_to”关系表示声明该模型为其所属模型拥有外键的模型。 So, Department belongs_to Company. 因此,部门属于公司。 Thus, :company_id is created inside Department when you type, say, rails generate model Department company:references....etc. 因此,当您键入,例如,rails生成模型Department company:references ....等时,:company_id将在Department内部创建。

In the example above, your Employee will have a :department_id and a :company_id, though, again, you may skip the company one and just declare it as a through and you will look up your employees through the departments they are in. 在上面的示例中,您的员工将具有:department_id和:company_id,不过,再次重申,您可以跳过公司之一,只是将其声明为直通公司,然后将通过其所在部门查找员工。

You could, in theory, also use a has_one instead of a belongs_to for employee. 从理论上讲,您也可以为员工使用has_one而不是belongs_to。 But I find those harder to work with. 但是我发现那些更难处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM