我想在创建一个特定的Proposal
之后创建一个Project
我像这样创建项目......
项目控制器.rb
...
def create
@project = current_user.organization.projects.build(params[:project])
if @project.save
redirect_to current_user.organization
else
render :new
end
end
...
在项目构建之前,我想创建它的关联Proposal
项目.rb
class Project < ActiveRecord::Base
...
has_one :proposal
after_save :build_project_proposal
private
def build_project_proposal
# self => #<Project id: nil, name: "FourthProject", organization_id: 1...>
# ideally i would like to call something like
# self.build_proposal but without an id, its not producing the association
end
为什么after_save
回调会产生一个nil
id?我也使用 Observers 得到了相同的结果。