# How to save multiple data

**URL:** <https://www.ruby-forum.com/t/how-to-save-multiple-data/114701>\
**Category:** Rails\
**Created:** [September 17, 2007, 1:24pm UTC](https://www.ruby-forum.com/t/how-to-save-multiple-data/114701 "2007-09-17T13:24:31Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![SMARTEGURU](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a3d4f5/32.png) [@SMARTEGURU](https://www.ruby-forum.com/u/SMARTEGURU)\
**Post date:** [September 17, 2007, 1:24pm UTC](https://www.ruby-forum.com/t/how-to-save-multiple-data/114701/1 "2007-09-17T13:24:31Z")

</div>

hi,

i have 3 models:

class Company \< ActiveRecord::Base  
has\_many :company\_branches  
end

class CompanyBranch \< ActiveRecord::Base  
has\_many :branch\_contacts  
belongs\_to :companies  
end

class BranchContact \< ActiveRecord::Base  
belongs\_to :company\_branches  
end

i am getting following parameter in action: form can add multiple  
branches and their contacts dynamically

Parameters: {“company”=\>{“company\_type\_id”=\>“1”,  
“company\_name”=\>“c22222222”, “head\_office\_address”=\>“cxczxczx”,  
“notes”=\>“ewewq”}, “commit”=\>“Create”,  
“contact\_information”=\>{“0”=\>[“b1c1”, “b1c2”], “1”=\>[“b2c1”, “b2c2”]},  
“branch\_detail”=\>{“0”=\>“b1”, “1”=\>“b2”}}

how to save them in respective table

i tried

if @company.save  
all\_branchs = params[:branch\_detail]  
all\_branchs.each do |q|  
param\_branch = {:branch\_detail =\> q[1]}  
@company\_branches = CompanyBranch.new(param\_branch)  
@company\_branches.company\_id = @company.id  
if @company\_branches.save  
all\_branch\_contacts=nil  
all\_branch\_contacts = params[:contact\_information]  
logger.debug all\_branch\_contacts.to\_yaml  
all\_branch\_contacts.each do |p|  
param\_hash = {:contact\_information =\> p[1]}  
@branch\_contacts=BranchContact.new(param\_hash)  
@branch\_contacts.branch\_id = @company\_branches.id  
@branch\_contacts.save  
end  
end  
end  
end

but it’s saving junk in contact table.

Thanks in advance
