3

我有以下模型和关联:

class JuridicalPerson < ActiveRecord::Base
end

class Supplier < ActiveRecord::Base
  belongs_to :juridical_person
  delegate  :company_name, :company_name=, :to => jurirical_person
end

控制器是:

def new
  @supplier = Supplier.new                                                                                                                                                 

  respond_to do |format|
    format.html # new.html.erb
    format.json { render json: @supplier }
  end
end

架构如下所示:

create_table "suppliers", :force => true do |t|
  t.integer  "juridical_person_id"
  ...
end

create_table "juridical_people", :force => true do |t|
  t.string   "company_name"
  ...
end

现在,当我尝试在视图中渲染它时,出现以下错误:

供应商#company_name 委托给 juridical_person.company_name,但 juridical_person 为 nil: #(Supplier id: nil, juridical_person_id: nil, created_at: nil, updated_at: nil)

提取的源代码(在第 9 行附近):

8:       <%= f.label :company_name, "Company Name" %>
9:       <%= f.text_field :company_name %>

似乎在委派时没有创建相关的 juridical_person ,但我不知道为什么。即使我在控制器中创建它,出于同样的原因尝试更新时应用程序也会中断。我错过了什么?

4

2 回答 2

0

删除=更改

delegate  :company_name, :company_name=, :to => jurirical_person

delegate  :company_name, :company_name, :to => jurirical_person
于 2012-09-17T05:42:36.063 回答
0
class JuridicalPerson < ActiveRecord::Base
    has_many :suppliers
end
于 2012-09-17T07:24:11.037 回答