I have started learning Ruby on Rails and I have been stuck at something and need you help. To learn Rails, I am building an app for creating, filling and reporting surveys.
My model is like – Each Survey has many Questions and each Survey has many Responses. Each Response has many Answers.
On my reports page controller, I am doing something like –
def index
#@survey = Survey.find(params[:survey_id])
@user_responses = Response.where("survey_id = ?", params[:survey_id])
end
and on the view –
<% @user_responses.each do |response| %>
Responder Name - <%= response.responder_name %><br/>
<% response.answers do |answer|%>
Question Id - <%= answerx.question_id %>
Answer - <%= answer.answer_text %>
<% end%>
<% end %>
I can see the query being executed on the backend (due to the loop on the view), e.g.
[1m[36mResponse Load (0.3ms)[0m [1mSELECT "responses".* FROM "responses" WHERE (survey_id = '14')[0m
[1m[35mAnswer Load (0.3ms)[0m SELECT "answers".* FROM "answers" WHERE ("answers".response_id = 14)
[1m[36mAnswer Load (0.2ms)[0m [1mSELECT "answers".* FROM "answers" WHERE ("answers".response_id = 15)
But on the view I am not able to see the “answer_text”/”question_id”. I can only see “responder_name”
This has something to do with queries being executed from the view ?
Please help me.
Thanks.
I think you want:
With an
each.