我希望在后创建Proposal
我创建一个特定的Project
我像这样创建项目......
projects_controller.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
project.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?我也使用观察者获得了相同的结果。