"프로세스가 변경되면 이러한 구조를 업데이트하십시오"라는 말은 데이터베이스의 필드가 변경 될 때 CSV 또는 YAML 데이터를 읽고 쓰는 코드를 변경한다는 의미입니까?
다음 코드는 기록 및 CSV로/모든 AR 오브젝트를 읽어합니다 (FasterCSV 보석 필요) :
def load_from_csv(csv_filename, poro_class)
headers_read = []
first_record = true
num_headers = 0
transaction do
FCSV.foreach(csv_filename) do |row|
if first_record
headers_read = row
num_headers = headers_read.length
first_record = false
else
hash_values = {}
for col_index in 0...num_headers
hash_values[headers_read[col_index]] = row[col_index]
end
new_poro_obj = poro_class.new(hash_values) # assumes that your PORO has a constructor that accepts a hash. If not, you can do something like new_poro_obj.send(headers_read[col_index], row[col_index]) in the loop above
#work with your new_poro_obj
end
end
end
end
#objects is a list of ActiveRecord objects of the same class
def dump_to_csv(csv_filename, objects)
FCSV.open(csv_filename,'w') do |csv|
#get column names and write them as headers
col_names = objects[0].class.column_names()
csv << col_names
objects.each do |obj|
col_values = []
col_names.each do |col_name|
col_values.push obj[col_name]
end
csv << col_values
end
end
end
내가 "이러한 구조를 업데이트 프로세스가 변경되면"말할 때 내가 의미가 정확히 무엇을
것은 이것이! 이 코드는 작업을 손쉽게 수행 할 수 있습니다. 감사합니다. –