SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Hybrid View

  1. #1
    SitePoint Enthusiast
    Join Date
    Jun 2009
    Posts
    53
    Mentioned
    0 Post(s)
    Tagged
    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:

    Code:
    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.

  2. #2
    SitePoint Enthusiast
    Join Date
    Jun 2009
    Posts
    53
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    @assignment = Assignment.create(:start_date => project.start_date, :end_date => project.end_date, :project_id => project.id, :employee_id => project.project_manager, :notes => "Project Manager")
    fixes it .

  3. #3
    SitePoint Guru
    Join Date
    Jan 2001
    Location
    Alkmaar, Netherlands
    Posts
    710
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can delegate "project's start and end date" to project if you want from Assignment model if they will stay same as project(or just move them from project to here). Same goes for project manager.
    Code Ruby:
    class Assignment < ActiveRecord::Base
      belongs_to :project
      belongs_to :employee
      delegate :end_date, :to => :project
      delegate :start_date, :to => :project
      #....
    end
     
    #In your controller
    def create
    	@project = Project.new(params[:project])
    	if @project.save 
    	   @project.project_manager.assignments << Assignment.new(:project_id => project.id, :notes => "Project Manager")
                  redirect_to :action => 'index' 
    	else
    	   render :action => 'new'
    	end	
    end

  4. #4
    SitePoint Enthusiast
    Join Date
    Jun 2009
    Posts
    53
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oh nice - I've never seen delegate before. Would it be possible to use it within a conditional? Like, say I want to delegate the dates between assignment and project only IF an assignment's is_admin field is 1. Right now I'm using a "check" method and some sloppy parameter passing...

  5. #5
    SitePoint Addict
    Join Date
    Feb 2007
    Posts
    270
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by deliveryguy View Post
    Oh nice - I've never seen delegate before. Would it be possible to use it within a conditional? Like, say I want to delegate the dates between assignment and project only IF an assignment's is_admin field is 1. Right now I'm using a "check" method and some sloppy parameter passing...
    Use a before filter in your controller to check for admin before entering the create method. Then you can redirect non-admins to a login screen, and others never get near the create method. Better security *and* usabillity.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •