ruby on rails - assigning parent_id from an existing attribute -
i have database imported csv file , assign hierarchy based on assembly level below. not sure approach take. can write if statements long. sorry new this, , don't know begin. appreciated.
assembly_level assembly_tree description uniq_id 0 0 level "zero level" d9s64 1 level 1 assembly "level one" c9633 2 level 2 assembly "level two" 11197 3 level 3 assembly "level three" e271f 4 level 4 assembly "level four" 552da 4 level 4 assembly "level four" 4568a 3 level 3 assembly "level three" b72bd
how can assign assembly_level parent_id during import?
@item = item.new(:assembly_level=> params[:parent_id])
assuming importing content csv file reading line line , little information provide, consider (correct me if wrong) hierarchy implied in order of file.
i try this:
- whenever store record, store id of record in temporary variable.
- then, check if next record children. if so, assign parent_id have stored.
a sample of unverified code, can see thinking:
previous_record['id'] = 0 previous_record['level'] = 999 # temp variable previous record csv_file.read |line| # reading method of choice line_array = line.split(";") item = item.new({'assembly_level' => line_array[0], 'assembly_tree' => line_array[1], 'description' => line_array[2], 'uniq_id' => line_array[3]) # creating regular item # if current item children assembly level greater previous 1 if item.assembly_level > previous_record['level'] item.parent_id = previous_record['id'] item.save else # store previous value when not children # storing values use next record previous_record['id'] = line_array[3] previous_record['level'] = line_array[0] end end
Comments
Post a Comment