【发布时间】:2016-01-18 21:41:12
【问题描述】:
我有一个项目模型,用于在创建时在特定项目中自动生成部门。这包含在projects 模型中:
class Project < ActiveRecord::Base
attr_accessible :title, :departments_attributes, :positions_attributes, :id
belongs_to :user
has_many :departments
has_many :positions
validates :title, presence: true
before_create :set_departments
accepts_nested_attributes_for :departments
accepts_nested_attributes_for :positions
private
def set_departments
self.departments.build department: "Test Dept", production_id: self.id
end
end
每个部门都有很多职位。我也在努力为部门创造职位。在此模型中,如何将新职位与部门关联?
【问题讨论】:
-
那么,项目有_many职位,部门有_many职位?
-
我把那条线放在那里是为了尝试这样做。我想正确的方法是项目
has_many: departments和部门has_many: positions。 -
是的,听起来不错。然后项目有_很多职位,通过:部门,好玩好玩!
标签: html ruby-on-rails ruby-on-rails-3 associations rails-activerecord