I have a nested form that processes the survey and its responses. But I get a strange error when loading the form:
ActiveRecord :: HasManyThroughNestedAssociationsAreReadonly
Any ideas? I am not sure how I should establish associations.
<%= form_for @survey do |f| %>
...
<%= f.fields_for :answers do |builder| %>
<%= builder.text_field :content, :class=>"form-control" %>
<% end %>
...
<% end %>
Review # new
def new
@survey = Survey.new
@template = Template.find(params[:template_id])
@patient = Patient.find(params[:patient_id])
@survey.answers.build
end
Survey.rb
class Survey < ActiveRecord::Base
belongs_to :template
has_many :questions, :through=> :template
has_many :answers, :through=> :questions
accepts_nested_attributes_for :answers
end
Template.rb
class Template < ActiveRecord::Base
belongs_to :survey
has_many :questions
end
Question.rb
class Question < ActiveRecord::Base
belongs_to :template
has_many :answers
end
Answer.rb
class Answer < ActiveRecord::Base
belongs_to :question
end
source
share