From 154674b993d59974016bc198e0cf267cb46400fb Mon Sep 17 00:00:00 2001 From: Grant Date: Mon, 8 Jul 2024 21:18:18 -0500 Subject: [PATCH] Update the seed script to grab half of the items with the lowest total quantity and for each one assign a random minimum and recommended value based off the max and min values in the total quantity --- db/seeds.rb | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/db/seeds.rb b/db/seeds.rb index ad76bd2575..1e36634a7d 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -79,9 +79,20 @@ def random_record_for_org(org, klass) # Set minimum and recomended inventory levels for items at the Pawnee Diaper Bank Organization half_items_count = (pdx_org.items.count/2).to_i -min_value = 1500 -recomended_value = 2000 -pdx_org.items.limit(half_items_count).update_all(on_hand_minimum_quantity: min_value, on_hand_recommended_quantity: recomended_value) +low_items = pdx_org.items.left_joins(:inventory_items) + .select('items.*, SUM(inventory_items.quantity) AS total_quantity') + .group('items.id') + .order('total_quantity') + .limit(half_items_count) + +min_qty = low_items.first.total_quantity +max_qty = low_items.last.total_quantity + +low_items.each do |item| + min_value = rand((min_qty / 10).floor..(max_qty/10).ceil) * 10 + recomended_value = rand((min_value/10).ceil..1000) * 10 + item.update(on_hand_minimum_quantity: min_value, on_hand_recommended_quantity: recomended_value) +end # ---------------------------------------------------------------------------- # Request Units