Nested Forms not passing belongs_to :id

Posted by Bill Christian on Stack Overflow See other posts from Stack Overflow or by Bill Christian
Published on 2010-05-04T02:37:52Z Indexed on 2010/05/04 2:48 UTC
Read the original article Hit count: 498

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?

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about nested-attributes