2

I'm building a survey app and part of that app has two models:

#ResponseSet 
has_many :responses
accepts_nested_attributes_for :responses

#Response 
belongs_to :response_set
belongs_to :answer

As an example, you might have a survey with a multiple choice question where you can select multiple answers (What types of pets do you have? Dog, cat, horse).

What is supposed to happen is that when you submit your answers for a survey, a record is added to the Response table for each answer. So if you selected that you had a dog, cat and horse, 3 records would be added, each one referencing the Answer model via :answer_id.

I can do it for questions with one answer, but ones with multiple answers is breaking my brain.

It gets more complicated because in a single survey there could be multiple questions each with multiple answers.

For reference, current form field names look like this:

response_set[responses_attributes][0][answer_id]
response_set[responses_attributes][1][answer_id]
response_set[responses_attributes][2][answer_id]

Generated by this:

<%= form_for @response_set, :url => complete_survey_path do |f| %>
  <%= f.fields_for :responses do |response| %>
    <%= response.check_box :answer_id %>
  <% end %>
<% end %>

So that'd be the field for answer_id in the response_attributes for 3 different questions.

4
  • Have you watched railscasts.com/episodes/196-nested-model-form-part-1 episode? It explains nested form with the same topic. Commented Jun 27, 2011 at 16:03
  • I have indeed. It's not the same issue (as far as I can tell). I'm asking how to get the ID of an already existing record as part of the field name (see my example). Commented Jun 27, 2011 at 17:34
  • If 1234 is the id of your answer, response_set[responses_attributes][0][1234][answer_id] looks a lot like response_set[responses_attributes][0][answer_id][answer_id]. That doesn't look right to me. Commented Jun 29, 2011 at 20:14
  • I just rewrote the whole question to clarify the problem. Sorry about all that. Commented Jun 29, 2011 at 21:36

1 Answer 1

1

The problem seems a little fuzzy to me without seeing the associations for Answer, the code for the create action, and the full generated HTML, but I believe this article by Shelly Roche provides a solution to your problem--perhaps with some refactoring to your model associations (i.e. ResponseSet has_many :responses, :through => :answers).

Unfortunately, the snippet she provides of her form.fields_for seems incomplete; the HAML ends with a break tag, but, in the HTML, the key bit (a hidden input field that ids the attribute associated with the check box) comes after a break tag.

Also, I can't say if the hidden fields containing the ids would be picked up automatically when saving the response, or if you would need to pull the fields out of params and process them manually.

But I'd wager that Shelly Roche would be willing to complete the snippets for you if you care to comment on her post.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.