Reputation: 708
I have the following models and associations:
class JuridicalPerson < ActiveRecord::Base
end
class Supplier < ActiveRecord::Base
belongs_to :juridical_person
delegate :company_name, :company_name=, :to => jurirical_person
end
The controler is:
def new
@supplier = Supplier.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: @supplier }
end
end
The schema looks as follow:
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
Now when I try to render it in a view, I get the following error:
Supplier#company_name delegated to juridical_person.company_name, but juridical_person is nil: #(Supplier id: nil, juridical_person_id: nil, created_at: nil, updated_at: nil)
Extracted source (around line #9):
8: <%= f.label :company_name, "Company Name" %>
9: <%= f.text_field :company_name %>
Seems like the associated juridical_person is not being created at the time of delegation, but I can't figure out why. Even if I create it in the controller, the app will break when trying to update for the same reason. What am I missing?
Upvotes: 3
Views: 528
Reputation: 860
class JuridicalPerson < ActiveRecord::Base
has_many :suppliers
end
Upvotes: 0
Reputation: 47532
remove =
Change
delegate :company_name, :company_name=, :to => jurirical_person
To
delegate :company_name, :company_name, :to => jurirical_person
Upvotes: 0