ruby-on-rails - 如何将csv文件复选框值导出到ruby on rails的表中?
问题描述
school_type col 复选框数据将 csv 文件导出到表中。我在我的代码下试过
学生.rb
# for csv file
def self.to_csv
attributes = %w{id status email temp_registration_date full_name full_name_kana main_registration_date email2 postal_code1 postal_code2 address1 address2 tel_main tel_sub sex school_type school_name school_name_initial department_name faculty_type graduation_month
industry_id industry_most_id occupation_id occupation_most_id desired_work_region_code club_flg school_club_name school_club_type club_name lab_name major_field
research_subject_title research_subject_summary interested_companies personal_summary how_known how_known_other mail_magazine_flg}
CSV.generate(headers: true) do |csv|
csv << attributes
all.each do |student|
csv << [student.id,student.name, student.sex, if (student.school_type == 1)
大学
elsif (student.school_type == 2)
大学院(修士)
elsif (student.school_type == 3)
大学院(博士)
elsif (student.school_type == 4)
短大
elsif (student.school_type == 5)
専門学校
elsif (student.school_type == 6)
高専
end]
end
end
我有我的桌子
解决方案
你可能需要更多这样的东西:
attributes = %w{id status email...}
CSV.generate(headers: true) do |csv|
# Build headers
csv << attributes
all.each do |student|
# use a variable to set the school_type string
school_type_text = if (student.school_type == 1)
"大学"
elsif (student.school_type == 2)
"大学院(修士)"
elsif (student.school_type == 3)
"大学院(博士)"
elsif (student.school_type == 4)
"短大"
elsif (student.school_type == 5)
"専門学校"
elsif (student.school_type == 6)
"高専"
end
csv << [student.id, student.name, student.sex, school_type_text]
end
end
我在OpenStruct.new({school_type: 2, id: 1, name: 'test'})
(不是在 Rails 中)上运行它,输出是:
"id,status,email,temp_registration_date,full_name,full_name_kana,main_registration_date,email2,postal_code1,postal_code2,address1,address2,tel_main,tel_sub,sex,school_type,school_name,school_name_initial,department_name,faculty_type,graduation_month,industry_id,industry_most_id,occupation_id,occupation_most_id,desired_work_region_code,club_flg,school_club_name,school_club_type,club_name,lab_name,major_field,research_subject_title,research_subject_summary,interested_companies,personal_summary,how_known,how_known_other,mail_magazine_flg
\n1,test,something,大学院(修士)\n"
推荐阅读
- android - ContentResolver.openAssetFileDescriptor 抛出 java.io.FileNotFoundException: No entry for content://media/external/audio/media
- spring - 如何在spring boot中声明多个具有相同类但使用不同属性的对象
- java - 如何获取在 Android 的列表视图中单击的项目的字符串值
- xslt - 删除 XSLT 中两个字符串和节点之间的空格
- python - 如何获取每个标签值对应每个`user_id`的比例
- sql - Oracle SQL 我无法分组
- python - 如何将第 i 行的 numpy 数组的单个元素与第 i 行的另一个 numpy 数组的元素相乘?
- http - 如何处理python请求中的“无法建立新连接”错误
- groovy - Groovy 闭包中的字符串引用导致的内存使用
- python - 将现有的 Shell 代码转换为 Python 以进行文件移动