Bulk Import in Ruby On Rails older versions

record.valid?
# Example of a before calls
before_update :set_extra_data,
def self.skip_before_callbacks?
@skip_before_callbacks
end

def self.skip_before_callbacks=(value)
@skip_before_callbacks = value
end
def set_extra_data
return if self.class.skip_before_callbacks?
#some operations
end
def run_required_callbacks(active_records, is_after = false)
return if active_records.blank?
active_records.each do |record|
# import_id will be blank in case we already have ID column populated (Update case).
if is_after # after callbacks
record
.import_id.blank? ? (record.run_callbacks(:update) { true }) : (record.run_callbacks(:create) { true })
record.run_callbacks(:save) { true }
else # before callbacks
record
.run_callbacks(:save) { false }
record.import_id.blank? ? (record.run_callbacks(:update) { false }) : (record.run_callbacks(:create) { false })
end
end
end
def get_all_changes(records)
create_case_changes = {}
update_case_changes = {}
records.each do |record|
record.new_record? ? (create_case_changes[record.import_id.to_s] = record.changes) : (update_case_changes[record.id.to_s] = record.changes)
end
[create_case_changes, update_case_changes]
end
def execute_bulk_insert(klass, records, retry_count = 0, exception = nil)
raise exception.message if retry_count > 2
begin
klass.import records, validate: false, on_duplicate_key_update: klass.columns.collect(&:name)
rescue => exception
Rails.logger.info exception.message
return execute_bulk_insert(klass, records, retry_count + 1, exception)
end
end
model.reflections
def set_previous_changes(imported_records, create_case_changes, update_case_changes)
imported_records.each do |record|
if record.import_id.blank?
record.instance_variable_set(:@previously_changed, update_case_changes[record.id.to_s])
record.instance_variable_set(:@_start_transaction_state, new_record: false, id: record.id)
else
record.instance_variable_set(:@previously_changed, create_case_changes[record.import_id])
record
.instance_variable_set(:@_start_transaction_state, new_record: true)
end
end
end
run_required_callbacks(imported_records, true)
ActiveRecord::Base.transaction do
records = import_records(*)
end
def run_after_commit_callback(records)
records.each do |record|
begin
record.run_callbacks(:commit) { true }
rescue => e
Rails.logger.error "Error while running after commit, Message => #{e.message}, Trace => #{e.backtrace.join('\n')}"
end
end
end

--

--

--

Programming Nerd ashishm.dev

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Ashish Muralidharan

Ashish Muralidharan

Programming Nerd ashishm.dev

More from Medium

Dealing with rich text in Rails 7

TimedSemaphore Implementation in Ruby

Auto Save Action Text Data in Rails

MacBook Air on gray armchair

Utilizing both frontend and backend validations in your React/Ruby on Rails Web Application