collectiveidea/awesome_nested_set

How make bulk insert and create the tree whitout use rebuild!

Closed this issue · 3 comments

I'm refactor my code with bulk_insert for a tree (i'm already using awesome_nested_set) and I have an array with several objects, where they only have the parent_id.

Actually i'm using rebuild inside a Job. But I think that is not an efficient solution. So, I appreciate if you have any ideas to improve my code.

I use bulk_insert gem.

Job

class MassiveInsertJob < ApplicationJob
  queue_as :default

  def perform (task, structure)
    Task.create_rhytmic_task(task, structure)
    Task.rebuild!
    begin
      SendPusherJob.perform_later(task, structure, {message: 'Rhythmic task ok', type: 'notice'})
    rescue => error
      puts error
    end
  end
end

Bulk

Task.bulk_insert(:name, :unique_id, :owner_id, :sector_id, :current_progress,
                    :base_start_date, :base_end_date, :base_duration,
                    :base_days_duration, :start_date, :end_date, :duration,
                    :days_duration, :current_state, :completion_status,
                    :reschedules_count, :source, :project_gantt_id, :rhytmic_task,
                    :active, :calendar_unique_id, :parent_id, :lft, :rgt,
                    :created_at, :updated_at)  do |worker|
     array_children.each do |task|
       array = [task[:name], task[:unique_id], task[:owner_id], task[:sector_id],
                task[:current_progress], task[:base_start_date],
                task[:base_end_date], task[:base_duration], task[:base_days_duration],
                task[:start_date], task[:end_date], task[:duration],
                task[:days_duration], task[:current_state],
                task[:completion_status], task[:reschedules_count],
                task[:source], task[:project_gantt_id], task[:rhytmic_task],
                task[:active], task[:calendar_unique_id],
                task[:parent_id], task[:lft], task[:rgt], DateTime.now, DateTime.now]
       worker.add array
     end
   end

is it possible to use scope and rebuild a single scope?

drewB commented

You can see how we handled this in #326 (comment)

stale commented

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.