How to save multiple data

SMARTEGURU
Mon, 17 Sep 2007 06:02:35 -0000

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


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---