0
我有以下型號嵌套表格不及格belongs_to的:ID
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在每個嵌套成員記錄?或者當創建成員記錄時,控制器需要填充什麼?
這是否必要是@project已經被保存到數據庫?我正在嘗試克隆一個現有的項目,並且一次保存一個新的project_id。 – 2010-05-04 19:48:39