【发布时间】:2012-12-26 13:48:32
【问题描述】:
我正在使用 Ruby on Rails 3。并试图绘制出三个模型来模拟公司、员工及其各自部门的数据。
在得出以下解决方案:
class Company < ActiveRecord::Base
has_many :departments
has_many :employees, through => :departments
end
class Department < ActiveRecord::Base
belongs_to :company
has_many :employees
has_one :department_description
end
class DepartmentDescription < ActiveRecord::Base
belongs_to :department
end
class Employee < ActiveRecord::Base
belongs_to :department
end
这是关联这些模型的“正确”方式吗?
【问题讨论】:
-
没有理由(鉴于您提供的上下文)为什么您应该为
Department的:description提供一个单独的表/模型。将该列添加到Department并摆脱:department_description上的has_one关系 -
是的,我没有添加任何上下文。但是情况是 has_many :employees,通过 => :departments 有效地使部门表 (DB) 成为连接表,而 DepartmentDescription (姓名、电子邮件等)将是多余的。
标签: ruby-on-rails associations has-many-through