Controller and View in a many-to-many relationship

Hi Guys!
I’m new to Rails so apologize if I make any newbie mistake…

I have 3 models: Ideia, Pergunta (Question in portuguese),
Resposta(answer in portuguese)
I’ll translate the names so it’ easier for you to understand the ideia.
But basically, a person can create an ideia, and answer some questions
about this ideia. The questions are inserted in the system and all the
users should answer these questions when creating an ideia.

Idea has_many :answers
Question has_many :answers
Answer belongs_to :ideia, belongs_to :question

What I want to do is when a user creates an ideia, it will load all the
available questions and user can answer it when he’s creating the

I’m trying to do something like this:

<%= form_for(@ideia) do |f| %>
<% @perguntas = Pergunta.where("father_id IS NULL") %>
  <% for pergunta in @perguntas %>
    <%= pergunta.titulo %>

    <% f.fields_for :respostas do |builder| %>
      <div class="field">
        <%= builder.label :descricao %><br />
        <%= builder.text_area :descricao, :rows => 3 %>
        <%= builder.check_box :_destroy %>
        <%= builder.label :_destroy, "Remove Resposta" %>
    <% end %>

  <% end %>
<% end %>

It doesn’t work because I don’t know how to “link” the models. What
could I do? Any suggestion?

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs