Compare commits

...

2 Commits

Author SHA1 Message Date
Daniel O'Connor
10e5fb8fb3 Merge branch 'dev' into fix/preserve-scientific-name-attributes 2026-04-25 10:10:04 +09:30
google-labs-jules[bot]
82a9b58ab3 Fix: Preserve scientific name attributes on crop edit
When editing a crop, the `recreate_names` method in the `CropsController`
would always destroy and recreate the associated scientific names, even if
they had not changed. This caused any additional attributes on the
scientific names to be lost.

This commit modifies `recreate_names` to first check if the submitted
names are different from the existing ones. If they are the same, the
method returns early, preserving the existing records and their
attributes.

Additionally, this commit corrects a typo in `crop_params` where
`:scientific_name` was used instead of `:name` for the nested attributes,
ensuring that the parameters are permitted correctly.
2025-12-02 13:09:36 +00:00

View File

@@ -204,6 +204,15 @@ class CropsController < ApplicationController
def recreate_names(param_name, name_type)
return if params[param_name].blank?
# Get the submitted names, reject blanks, and sort for comparison
submitted_names = params[param_name].values.reject(&:blank?).sort
# Get the existing names from the database, and sort for comparison
existing_names = @crop.send("#{name_type}_names").pluck(:name).sort
# Return early to prevent destroying and recreating names (and their associated attributes)
# if the list of names has not changed.
return if submitted_names == existing_names
@crop.send("#{name_type}_names").each(&:destroy)
params[param_name].each_value do |value|
next if value.empty?
@@ -226,7 +235,7 @@ class CropsController < ApplicationController
:public_food_key,
:row_spacing, :spread, :height,
:sowing_method, :sun_requirements, :growing_degree_days,
scientific_names_attributes: %i(scientific_name _destroy id)
scientific_names_attributes: %i(name _destroy id)
)
end