Skip to content

Commit d293515

Browse files
fix(association): return bad request when associated record is not destroy (#688)
1 parent 9ea0141 commit d293515

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

app/controllers/forest_liana/associations_controller.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ def dissociate
7676
dissociator.perform
7777

7878
head :no_content
79+
rescue ActiveRecord::RecordNotDestroyed => error
80+
render json: { errors: [{ status: :bad_request, detail: error.message }] }, status: :bad_request
7981
rescue => error
8082
FOREST_REPORTER.report error
8183
FOREST_LOGGER.error "Association Dissociate error: #{error}\n#{format_stacktrace(error)}"

app/services/forest_liana/has_many_dissociator.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,12 @@ def perform
3131

3232
if @with_deletion
3333
record_ids = record_ids.select { |record_id| @association.klass.exists?(record_id) }
34-
@association.klass.destroy(record_ids)
34+
@resource.transaction do
35+
record_ids.each do |id|
36+
record = @association.klass.find(id)
37+
record.destroy!
38+
end
39+
end
3540
end
3641
end
3742
end

0 commit comments

Comments
 (0)