我已经将这个csv文件定义为两个模块来获取数据 导出,现在我需要这些数据在任何一张单打表单中 建议我如何将此代码合并为单个CSV,因为对我而言 抛出一个错误,我也同时定义了两个控制器。
def self.to_csv
attributes = %w{ Employee-Code Employee-Name Entity Department-Code Designation-Code Date-Of-Joining Primary-Phone Secondary-Phone Emergency-Phone Primary-Email Secondary-Email Status}
CSV.generate(headers: true) do |csv|
csv << attributes
Employee.all.each do |employee|
csv << [employee.emp_code,employee.emp_name,employee.entity.entity_code,employee.department.depart_code,employee.designation.designation_code,employee.date_of_joining,employee.primary_phone,employee.secondary_phone,employee.emergency_phone,employee.primary_email,employee.secondary_email,employee.status]
csv.sort_by { |employee| employee['emp_code']}
end
end
end
这是一个雇员模块,它在employee_table im中存储数据 仅从该表中提取数据
def self.to_csv
attributes = %w{ Employee-code Date-Of-Birth Sex Marital-Status Anniversary-Date Permanent-Addr Communication-Addr State City Years-Of-Experience Height Weight Bank-Name Account-Number IFSC-Code Blood-Group}
CSV.generate(headers: true) do |csv|
csv << attributes
PersonalInfo.all.each do |personalinfo|
csv << [personalinfo.employee.emp_code,personalinfo.date_of_birth,personalinfo.sex,personalinfo.marital_status,personalinfo.anniversary_date,personalinfo.permanent_addr,personalinfo.commn_addr,personalinfo.years_of_experience,personalinfo.height,personalinfo.weight,personalinfo.acc_no,personalinfo.ifsc_code,personalinfo.blood_group]
end
end
end
这是另一个模块,其中数据存储个人表即时提取 从现在开始我只想将这两个csv文件加入单个csv ... 任何人都可以建议我怎么做,对于这两个模块的两个动作 控制器我写的和路由也基于....如何 合并这两个文件
答案 0 :(得分:0)
取决于你想要实现的目标,但如果你想合并这两个文件,我会做this SO answer中提出的建议。
编辑:这是代码
f1 = File.read('file1.csv')
f2 = File.read('file2.csv')
File.write('file3.csv', f1 + f2)