我有以下模型和关联:
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:#(供应商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,但我无法弄清楚原因。即使我在控制器中创建它,应用程序也会因为同样的原因而在尝试更新时中断。我错过了什么?
答案 0 :(得分:0)
删除=
更改
delegate :company_name, :company_name=, :to => jurirical_person
要
delegate :company_name, :company_name, :to => jurirical_person
答案 1 :(得分:0)
class JuridicalPerson < ActiveRecord::Base
has_many :suppliers
end