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 开发者_Go百科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?
Create the Member
object like this:
@member = @project.members.build
精彩评论