我有以下型号
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
我假设控制器将在 project.save 上为每个嵌套成员记录填充 member.project_id。但是,我收到一个验证错误,指出 project_id 为空白。
我的控制器方法:
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
我需要在每个嵌套成员记录中手动设置 project_id 吗?或者控制器在创建成员记录时需要填充什么?