The title is obviously confusing so let me get down to it.
class Resume < ActiveRecord::Base
has_many :references
end
class Reference < ActiveRecord::Base
has_one :phone
end
class Phone < ActiveRecord::Base
end
So I'm using cocoon and to have the simple_fields_for :phone
populated I need to run build_phone on every reference object created.
Similarly I had resume has_one basic_info has_one phone. And I was able to build it like so
@resume.build_basic_info
@resume.basic_info.build_phone
But in this case I have:
@resume.references.build
@resume.references.first.build_phone
Give me the error unknown attribute: reference_id
.
So how do I build the phone association for references in the cocoon form and have it built on every instantiation of a new reference in cocoon?
Edit: I didn't have reference_id
in phone. And thanks to @vee I have a much better way of handling existing models. So the code works now.
Still need to figure out how to run build_phone on every new call to link_to_add_association
in cocoon.
You can pass a block when you build references as:
@resume.references.build do |r|
r.build_phone
end