在after_create回调中没有创建Rails对象

时间:2013-06-10 22:14:01

标签: ruby-on-rails ruby-on-rails-3 callback

我希望在后创建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?我也使用观察者获得了相同的结果。

0 个答案:

没有答案