プロジェクト

全般

プロフィール

« | » 

リビジョン fd7c3c3d

高徹 高橋 徹 さんが約6年前に追加

[phase-23]Add csv file importing

差分を表示:

app/models/glossary_term.rb
require 'csv'
class GlossaryTerm < ActiveRecord::Base
belongs_to :category, class_name: 'GlossaryCategory', foreign_key: 'category_id'
belongs_to :project
......
where 'rubi like ?', "#{sanitize_sql_like(keyword)}%"
}
def self.csv_attributes
["name", "name_en", "datatype", "codename", "description", "rubi", "abbr_whole"]
end
def self.import(file, project)
CSV.foreach(file.path, headers: true, encoding: "CP932:UTF-8" ) do |row|
term = new
term.attributes = row.to_hash.slice(*csv_attributes)
term.project = project
unless row["category"].blank?
term.category = GlossaryCategory.find_by(name: row["category"]) ||
GlossaryCategory.create(name: row["category"], project: project)
end
term.save!
end
end
end

他の形式にエクスポート: Unified diff