How to save multiple data

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs