Results 1 to 5 of 5
Thread: Creating two related Objects
Jul 9, 2009, 07:45 #1
- Join Date
- Jun 2009
- 0 Post(s)
- 0 Thread(s)
Creating two related Objects
Thanks for looking -
Upon the creation of a Project, the user selects a project_manager. This gets stored as an ID that maps to the employee_id of the Employee table. When this Project gets created, a new Assignment is also made between the Project and project_manager. I can't seem to get this working.
I've tried two things:
Creating a class method:
def create @project = Project.new(params[:project]) @employees = Employee.find(:all) if @project.save assignment_from_project(@project) else render :action => 'new' end end def assignment_from_project(project) @assignment = Assignment.new(:start_date => project.start_date, :end_date => project.end_date, :project_id => project.id, :employee_id => project.project_manager, :notes => "Project Manager") redirect_to :action => 'index' end
This doesn't generate any errors, but a new Assignment is not created.
Using an after_create method in the Projects model
This generates the Assignment, but the new Project's data is not seen by the model, so important attributes are not defined.
Any help would be awesome. Thanks.