[Rails] Fertilizer recipe solver for the FAPG.
1
Target.transaction do
2
wanted = {
3
"salades" => 40,
4
"tomates général" => 40,
5
"développement floral" => 20
6
}
7
8
# Ensure all referenced profiles exist
9
profiles = NutrientProfile.where(name: wanted.keys).index_by(&:name)
10
missing = wanted.keys - profiles.keys
11
raise "Missing NutrientProfile(s): #{missing.join(', ')}" if missing.any?
12
13
target = Target.find_or_create_by!(name: "Cible par défaut")
14
15
target.target_allocations.destroy_all
16
wanted.each do |name, pct|
17
target.target_allocations.build(nutrient_profile: profiles[name], percentage: pct)
18
end
19
20
target.save!
21
end
22