2012-09-17 140 views
3

我有以下模型和协会:导轨3 - 模型关联(组合物)和方法委托

class JuridicalPerson < ActiveRecord::Base 
end 

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

CONTROLER是:

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是零:#(供应商ID:无,juridical_person_id:无,created_at:无,的updated_at:无)

提取的源(约行#9):

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

好像没有被在德时创建的相关联的juridical_person使馆,但我不明白为什么。即使我在控制器中创建它,当出于同样的原因尝试更新时,应用程序也会中断。我错过了什么?

回答

1

删除=变化

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

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

为什么假两个COMPANY_NAME代表团?顺便说一句,没有工作,但谢谢。 – eMgz

0
class JuridicalPerson < ActiveRecord::Base 
    has_many :suppliers 
end 
+0

我看不到逻辑,因为我没有访问JuridicalPerson类中的供应商集合。无论如何,我试过了,但它没有奏效,但谢谢。 – eMgz

+0

是的,它实际上看起来像你的问题是,你正在尝试为尚未创建的juridical_person设置company_name。您可能需要查看http://railscasts.com/episodes/196-nested-model-form-revised的accepts_nested_attributes_ –