@@ -81,7 +81,6 @@ def instrument_denormalized_attribute_from_through(attribute_name, from:, throug
8181 before_update -> { write_denormalized_attribute_from_persisted_record_through ( through_association_name , from , attribute_name , prefixed_attribute_name ) } , if : association_changed
8282
8383 # make sure validation fails if our denormalized column is modified directly
84-
8584 validate -> { validate_denormalized_attribute_from_record_through ( through_association_name , from , attribute_name , prefixed_attribute_name ) } , if : :"#{ prefixed_attribute_name } _changed?" , on : :update
8685
8786 denormalized_attributes << prefixed_attribute_name . to_sym
@@ -352,20 +351,22 @@ def write_denormalized_attribute_from_schrodingers_record_through(through_associ
352351 def validate_denormalized_attribute_from_persisted_record ( source_association_name , source_attribute_name , target_attribute_name )
353352 record = send ( source_association_name )
354353
355- unless read_attribute ( target_attribute_name ) == record &.read_attribute ( source_attribute_name )
356- if target_reflection = self . class . reflect_on_all_associations . find { it . foreign_key == target_attribute_name . to_s }
357- errors . add target_reflection . name , :not_allowed , message : 'cannot be modified directly because it is a denormalized association'
358- else
359- errors . add target_attribute_name , :not_allowed , message : 'cannot be modified directly because it is a denormalized attribute'
360- end
361- end
354+ validate_denormalized_attribute_from_record ( record , source_attribute_name , target_attribute_name )
362355 end
363356
364357 def validate_denormalized_attribute_from_record_through ( through_association_name , source_name , source_attribute_name , target_attribute_name )
365358 record = send ( through_association_name ) &.send ( source_name )
366359
360+ validate_denormalized_attribute_from_record ( record , source_attribute_name , target_attribute_name )
361+ end
362+
363+ def validate_denormalized_attribute_from_record ( record , source_attribute_name , target_attribute_name )
367364 unless read_attribute ( target_attribute_name ) == record &.read_attribute ( source_attribute_name )
368- errors . add target_attribute_name , :not_allowed , message : 'cannot be modified directly because it is a denormalized attribute'
365+ if target_reflection = self . class . reflect_on_all_associations . find { it . foreign_key == target_attribute_name . to_s }
366+ errors . add target_reflection . name , :not_allowed , message : 'cannot be modified directly because it is a denormalized association'
367+ else
368+ errors . add target_attribute_name , :not_allowed , message : 'cannot be modified directly because it is a denormalized attribute'
369+ end
369370 end
370371 end
371372 end
0 commit comments