# Nested Model Forms

**URL:** <https://www.ruby-forum.com/t/nested-model-forms/190663>\
**Category:** Rails\
**Created:** [June 24, 2010, 8:09pm UTC](https://www.ruby-forum.com/t/nested-model-forms/190663 "2010-06-24T20:09:32Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ants\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/6de8d8/32.png) [@Ants\_P](https://www.ruby-forum.com/u/Ants_P)\
**Post date:** [June 24, 2010, 8:09pm UTC](https://www.ruby-forum.com/t/nested-model-forms/190663/1 "2010-06-24T20:09:32Z")

</div>

I’m trying to get a multi level nested form model working but have hit a  
problem. In one of my models, I have a habtm association and it’s  
throwing  
an error on a nested form and these are check boxes with a method of  
‘time\_unit\_ids[]’.

The nested forms work without the habtm (time\_unit\_ids[]) so things are  
pretty much set up okay.

Here’s the set up (obviously well and truly stripped back). Hopefully  
someone can see something I’m missing.

CIA -ants

Models:

event\_category:  
has\_many :event\_types  
accepts\_nested\_attributes\_for :event\_types

event\_type:  
has\_and\_belongs\_to\_many :time\_units  
has\_many :events  
belongs\_to :event\_category  
accepts\_nested\_attributes\_for :events

event:  
belongs\_to :event\_type

controller new action has …

```
@event_category.event_types.build
@event_category.event_types[0].events.build

```

Forms:  
event\_categories/new.html.erb  
\<% form … do |form| %\>  
\<% form.fields\_for :event\_types do |event\_type\_form| %\>  
\<% event\_type\_form.fields\_for :events do |event\_form| %\>

```
   <% @time_units.each do |time_unit| %>
      <% name = time_unit.name; id = time_unit.id %>
        <%= event_form.check_box 'time_unit_ids[]', { :id => name

```

}, time\_unit.id %\>  
\<% end %\>

undefined method `time\_unit\_ids[]’ for #EventType:0xb6fa13b8

I fear I may need to do this but it’s not ideal …

\<%= check\_box\_tag  
‘event\_category[event\_types\_attributes][0][time\_unit\_ids][]’,  
time\_unit.id,  
{ :id =\> name } %\>
