私は現在の協会を持っています:
Group :has_many Employees
とEmployee :belongs_to Group
しかし今、私は従業員を多くのグループにも関連付けたいと思っています。
この目的のために私は作ることを考えています:
groupizations group_id:integer employee_id:integer created_at:datetime
これにより、従業員モデルとグループモデルが変更されます。
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
同時にデータを追加する必要がありますか?