# Using nested model forms with polymorphic associations

**URL:** <https://www.ruby-forum.com/t/using-nested-model-forms-with-polymorphic-associations/166990>\
**Category:** Rails\
**Created:** [May 1, 2009, 7:07pm UTC](https://www.ruby-forum.com/t/using-nested-model-forms-with-polymorphic-associations/166990 "2009-05-01T19:07:45Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![bigdaddy71](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bbe5ce/32.png) [@bigdaddy71](https://www.ruby-forum.com/u/bigdaddy71)\
**Post date:** [May 1, 2009, 7:07pm UTC](https://www.ruby-forum.com/t/using-nested-model-forms-with-polymorphic-associations/166990/1 "2009-05-01T19:07:45Z")

</div>

Hello -

Sorry if this is long - I’m erring on the side of giving too much  
information rather than too little. Hopefully it’ll appear organized and  
easy to skim.

In a nutshell, I’m making a site where people can put together online  
tutorials. Each tutorial is broken up into sections, and there are two  
types of sections at the moment: Examples and Questions. I decided to do  
this with Polymorphic Associations, which look like this:

class Tutorial \< ActiveRecord::Base  
has\_many :sections, :order =\> ‘sections.position ASC’  
accepts\_nested\_attributes\_for :sections, :allow\_destroy =\> true

class Section \< ActiveRecord::Base  
belongs\_to :tutorial  
belongs\_to :sectionable, :polymorphic =\> true  
accepts\_nested\_attributes\_for :sectionable, :allow\_destroy =\> true

class Example \< ActiveRecord::Base  
has\_one :section, :as =\> :sectionable

class Question \< ActiveRecord::Base  
has\_one :section, :as =\> :sectionable

Defining a Section model this way has been very useful - it has a  
position attribute, so I can easily keep examples and questions ordered  
correctly, and it also has an “anchor” attribute, so that the user can  
define named anchors for the sections of each tutorial (i.e. “houses”  
for /tutorials/architecture#houses)

Now, I’m trying to set up a nested model form, so that when a tutorial  
is being edited, all the examples and questions are also presented (in  
the order they appear in the tutorial) and can be edited there, too.

The section portion of the edit tutorial page looks like:

\<% for section in @sections %\>  
\<% f.fields\_for section do |g| %\>  
\<%= g.label :anchor %\>\<br \>  
\<%= g.text\_field :anchor %\>

```
<% if section.sectionable_type == "Example" %>

  <% g.fields_for :sectionable do |h| %>
    <%= h.label :title %><br \>
    <%= h.text_field :title %>

    <%= h.label :content %><br \>
    <%= h.text_area :content %>
  <% end %>

<% elsif section.sectionable_type == "Question" %>

  <% g.fields_for :sectionable do |h| %>
    <%= h.label :title %><br \>
    <%= h.text_field :title %>

    <%= h.label :prompt %><br \>
    <%= h.text_area :prompt %>

    <%= h.label :answer %><br \>
    <%= h.text_area :answer %>
  <% end %>
<% end %>

```

\<% end %\>  
\<% end %\>

As far as I can tell, this is how the nested models are supposed to  
look, but this isn’t working. When I load up the edit tutorial page  
(I’ve already populated the database with with Lorem Ipsum-type stuff)  
it appears correctly - each example and question has their own edit box  
with the current information already filled in. But when I try to submit  
the form, I get:

unknown attribute: section

Previously, while tweaking, I’ve also gotten unknown attribute: example  
and unknown attribute: question. Looking at the source of the edit  
tutorial page, one of the spots where a question can be defined looks  
like:

Anchor\<br \>

Title\<br \>

Prompt\<br  
\>  
Autem et labore  
voluptas…

Answer\<br  
\>  
Qui consectetur  
beatae…

In other words, it doesn’t look like there’s anything in the html to  
differentiate the contents of one question from another - when the form  
is processed, the information from each question overwrites that from  
the previous question, and the final params hash is just a jumble of  
incomplete information.

I’m not sure how to fix this, or if nested model forms can even be used  
with polymorphic associations in this manner. Any ideas?

Thanks!  
Chris
