# Save has-many associations

**URL:** <https://www.ruby-forum.com/t/save-has-many-associations/184178>\
**Category:** Rails\
**Created:** [February 26, 2010, 5:31am UTC](https://www.ruby-forum.com/t/save-has-many-associations/184178 "2010-02-26T05:31:38Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![ash34](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/9de053/32.png) [@ash34](https://www.ruby-forum.com/u/ash34)\
**Post date:** [February 26, 2010, 5:31am UTC](https://www.ruby-forum.com/t/save-has-many-associations/184178/1 "2010-02-26T05:31:38Z")

</div>

I have the following association

class Document \< ActiveRecord::Base  
has\_many :sections  
has\_many :items, :through =\> :sections  
end  
class Section \< ActiveRecord::Base  
belongs\_to :document  
has\_many :items  
end  
class Item \< ActiveRecord::Base  
belongs\_to :section  
end

Here is the sample view for the ‘new’ action  
\<% form\_for(@document) do |f| %\>  
\<%= f.error\_messages %\>

Header

\<%= f.label :hdrcomment %\> \<%= f.text\_field :hdrcomment %\>

 \<% for section in @document.sections %\> \<% f.fields\_for section, :index =\> section do |s| %\> \<%= section.seqnum %\>  
 \<% for item in section.items %\> \<% s.fields\_for item, :index =\> item do |i| %\> \<%= i.text\_area :comments %\>  
 \<% end %\> \<% end %\> \<% end %\> \<% end %\> 

\<%= f.submit "Submit Document" %\>

\<% end %\>

What is way to save the header, sections and items. Do I have to iterate  
the request hash.  
The below code doesn’t work. Gives ActiveRecord::UnknownAttributeError  
inDocumentsController#create

def create  
@document = Document.new(params[:document])  
@document.save!  
end

Request:  
{“commit”=\>“Submit Document”,  
“authenticity\_token”=\>"+uOqJn1DSjzK6r1iIkaIqZa9VrKpU2Yr/A6zP0vg0eM=",  
“document”=\>{“section”=\>{"#Section:0xb74045f4"=\>{“item”=\>{"#Item:0xb73d2860"=\>{“comments”=\>“asdf”},  
“#Item:0xb73d1af0”=\>{“comments”=\>“sdf”}}},  
“#Section:0xb7403794”=\>{“item”=\>{"#FeedbackItem:0xb73ce6fc"=\>{“comments”=\>“sdf”}}},  
“#Section:0xb740ea90”=\>{“item”=\>{"#Item:0xb73e0e74"=\>{“comments”=\>“asd”}}}},  
“hdrcomment”=\>" asdf"}}

Thanks much.
