Skip to content

Commit

Permalink
PR review suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
leandroradusky committed Nov 2, 2023
1 parent c1a1182 commit 89af1a9
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions db/migrate/20231030102203_add_original_batch_id_to_samples.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,24 @@ def change
add_column :samples, :original_batch_id, :integer

Sample.preload(:batch).find_each do |sample|
batch = sample.batch || Batch.find_by(batch_number: sample.old_batch_number)
if batch
sample.original_batch_id = batch.id
if sample.batch
sample.original_batch_id = sample.batch.id
sample.save
elsif sample.box_id && sample.old_batch_number
institution_ids = TransferPackage
.select(:sender_institution_id)
.joins(:box_transfers)
.where(box_transfers: { box_id: sample.box_id })
.distinct

original_batch = Batch.where(
institution_id: institution_ids,
batch_number: sample.old_batch_number
).take
if original_batch
sample.original_batch_id = original_batch.id
sample.save
end
end
end
end
Expand Down

0 comments on commit 89af1a9

Please sign in to comment.