我目前有一个协会,其中:
Group :has_many Employees
和Employee :belongs_to Group
但现在我希望员工也与许多组相关联。
为此,我正在考虑制作:
groupizations group_id:integer employee_id:integer created_at:datetime
这将改变 Employee 和 Group 模型:
class Groupizations < ActiveRecord::Base
belongs_to :employee
belongs_to :group
end
class Group < ActiveRecord::Base
has_many :groupizations
has_many :employees, :through => categorizaitons
end
class Employee < ActiveRecord::Base
has_many :groupizations
has_many :groups, :through => categorizaitons
end
我从多对多的railscasts 插曲中了解了所有这些。我唯一感到困惑的是,现在我使用以下代码创建了一个新员工:
def create
@employee = Employee.new(params[:employee])
if @employee.save
flash[:notice] = "Successfully created employee."
redirect_to @employee
else
render :action => 'new'
end
end
这段代码将如何改变?我现在需要同时添加数据groupizations
吗?