I have a current association where:
Group :has_many Employees
and Employee :belongs_to Group
but now I want an Employee to be associated to many Groups as well.
For this purpose I am thinking of making:
groupizations group_id:integer employee_id:integer created_at:datetime
This will change Employee and Group models:
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
I understand all of this from railscasts episode on Many-to-Many. Only thing I am confused about is that right now I create a new Employee with following code:
def create
@employee = Employee.new(params[:employee])
if @employee.save
flash[:notice] = "Successfully created employee."
redirect_to @employee
else
render :action => 'new'
end
end
how will this code change? Do I need to add data into groupizations
at the same time now?