views:

84

answers:

1

I have the following model

class Project < ActiveRecord::Base
  has_many  :assignments, :conditions => {:deleted_at => nil}
  has_many  :members, :conditions => {:deleted_at => nil}
  accepts_nested_attributes_for :members, :allow_destroy => true
end

class Member < ActiveRecord::Base
  belongs_to :project
  belongs_to :person
  belongs_to :role

  has_many :assignments, :dependent => :destroy, :conditions => {:deleted_at => nil}
  accepts_nested_attributes_for :assignments, :allow_destroy => true


  validates_presence_of     :role_id
  validates_presence_of     :project_id
end

and I assume the controller will populate the member.project_id upon project.save for each nested member record. However, I get a validation error stating the project_id is blank.

My controller method:

  def create
    # @project is created in before_filter
    if @project.save
      flash[:notice] = "Successfully created project."
      redirect_to @project
    else
      render :action => 'new'
    end
  end

Do I need to manually set the project_id in each nested member record? Or what is necessary for the controller to populate when it creates the member records?

+1  A: 

Create the Member object like this:

@member = @project.members.build
Veeti
Does this necessitate that @project already be saved to the DB? I am trying to clone an existing project with its members and save with a new project_id all at once.
Bill Christian