Forum: Ruby on Rails Multi-model forms

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
865d16da95d6cd0be7442d09aad876d5?d=identicon&s=25 Dave Ekhaus (Guest)
on 2008-12-03 18:27
(Received via mailing list)
Hi

  I'm trying to implement multi-model forms.  Specifically a form
containing the following relationships ...

    parent -> children

    child  -> grandchildren

    [ NOTE : "->" denotes a one to many relationship / association -
the '>' points to the many ]

  Recipe #13 in Advanced Rails Recipes does a great job of explaining
how to implement the parent -> children relationship using Project
and Tasks.  Specifically ...

      project -> tasks  (using my "->" nomenclature)

  I'd like to include an additional model - for example adding
"Participants" to the above example ...

      project -> tasks

      task    -> participants

  Where I'm getting stuck is what to use for the 'prefix' (using the
terminology of Recipe #13) - or what name to use to refer to
'participants', in such a way that the params hash is properly
populated for 'auto-population' by the controller?

  Using the Project/Task metaphor and extending it to include the
relationship that a Task has many Participants, the 'task' partial
would look something like this (not in its final form - but early on
in the development of a +2 model multi-model form.  Also note the
'??????') ...

<div class="task">
  <% fields_for "project[task_attributes][]", task do |f| %>
    <p>
      Task: <%= f.text_field :name %>
    </p>

    <% 3.times { task.participants.build } %>

    <% for participant in task.participants %>

      <% fields_for "project[???????]", participant do |p_form| %>
        <p>
          Participant Name: <%= p_form.text_field :name  %>
        </p>
      <% end %>
    <% end %>
  <% end %>
</div>


  I'm assuming that a Task can have at most 3 participants, like what
was done in the early iterations of Ryan Bate's original screen-cast.

  I'm trying to figure out how to name the 'grandchildren' such that
the params hash is correctly populated ?   Specifically - what to put
in place of the '?????' in this line ...

    <% fields_for "project[???????]", participant do |p_form| %>

  If the solution doesn't involve replacing the '??????' above -
please point me in the right direction by providing as much detail /
info as you have time for.

  I'm grateful for any help / guidance you can provide.

Thanks
Dave
This topic is locked and can not be replied to.