3

Here is my model definition

class Partner < ActiveRecord::Base
   has_many :projectcollaborations   
   has_many :projects, :through => :projectcollaborations, :source => :project 
end

class Project < ActiveRecord::Base   
   has_many :projectcollaborations   
   has_many :partners, :through => :projectcollaborations, :source => :partner 
end

class Projectcollaboration < ActiveRecord::Base   
   belongs_to :project  
   belongs_to :partner 
end

My Joined Table Looks like the bellow one

+--------------+--------------+------+-----+-------------+-------+
| Field        | Type         | Null | Key | Default     | Extra |
+--------------+--------------+------+-----+-------------+-------+
| project_id   | int(11)      | NO   | PRI | 0           |       |
| partner_id   | int(11)      | NO   | PRI | 0           |       |
| project_role | varchar(255) | YES  |     | participant |       |
+--------------+--------------+------+-----+-------------+-------+

Now while creating a new project i want to pass the value "creator" in the project_role column. How can i achieve this ?

Current create method Looks like the bellow one

def create
    @partner = Partner.find(params[:partner_id])
    @project = Project.new(params[:project])
    @project.partners << @partner

    respond_to do |format|
      if @project.save
        format.html { redirect_to partner_project_path(@partner, @project), notice: 'Project was successfully created!' }
        format.json { render json: @project, status: :created, location: @project }
      else
        format.html { render action: "new" }
        format.json { render json: @project.errors, status: :unprocessable_entity }
      end
    end
  end
Kamrul Hassan
  • 294
  • 1
  • 2
  • 13
  • possible duplicate of [How to update a join model with a has_many :through association?](http://stackoverflow.com/questions/4248629/how-to-update-a-join-model-with-a-has-many-through-association) – Benoit Garret Oct 05 '11 at 13:01
  • Can I clarify what you mean by 'creator'? – cjm2671 Oct 05 '11 at 16:21
  • its just a value...for the column project_role for the relationship table projectcollaborations. my target is to create a new project, assign partner to it as a creator of the project. so that i can query later to find who is the creator of the project – Kamrul Hassan Oct 05 '11 at 17:20

0 Answers0