diff --git a/app/components/avo/index/resource_controls_component.rb b/app/components/avo/index/resource_controls_component.rb index ef2eadc359..0a8e0933c8 100644 --- a/app/components/avo/index/resource_controls_component.rb +++ b/app/components/avo/index/resource_controls_component.rb @@ -10,10 +10,7 @@ def initialize(resource: nil, reflection: nil, parent_model: nil, parent_resourc end def can_detach? - @reflection.present? && - @resource.model.present? && - is_has_many_association && - authorize_association_for(:detach) + is_has_many_association ? super : false end def can_edit? diff --git a/app/components/avo/resource_component.rb b/app/components/avo/resource_component.rb index 23cf6efcb4..c80d68de07 100644 --- a/app/components/avo/resource_component.rb +++ b/app/components/avo/resource_component.rb @@ -20,7 +20,14 @@ def can_delete? end def can_detach? - authorize_association_for(:detach) + return false if @reflection.blank? && @resource.model.blank? && !authorize_association_for(:detach) + + # If the inverse_of is a belongs_to, we need to check if it's optional in order to know if we can detach it. + if @reflection.inverse_of.is_a?(ActiveRecord::Reflection::BelongsToReflection) + @reflection.inverse_of.options[:optional] + else + true + end end def detach_path diff --git a/app/controllers/avo/associations_controller.rb b/app/controllers/avo/associations_controller.rb index 698b9a2597..fd66776d22 100644 --- a/app/controllers/avo/associations_controller.rb +++ b/app/controllers/avo/associations_controller.rb @@ -81,7 +81,7 @@ def destroy association_name = BaseResource.valid_association_name(@model, params[:related_name]) if reflection_class == "HasManyReflection" - @model.send(association_name).delete @attachment_model + @model.send(association_name).destroy @attachment_model else @model.send("#{association_name}=", nil) end