diff --git a/app/models/filter/resources.rb b/app/models/filter/resources.rb index 85950f857..bde1a30d1 100644 --- a/app/models/filter/resources.rb +++ b/app/models/filter/resources.rb @@ -12,6 +12,8 @@ module Filter::Resources kind = resource.class.model_name.plural send "#{kind}=", send(kind).without(resource) empty? ? destroy! : save! + rescue ActiveRecord::RecordNotUnique + destroy! end def buckets diff --git a/test/models/filter_test.rb b/test/models/filter_test.rb index 6d871326c..7635d0ff7 100644 --- a/test/models/filter_test.rb +++ b/test/models/filter_test.rb @@ -89,6 +89,11 @@ class FilterTest < ActiveSupport::TestCase assert_includes filter.as_params[:bucket_ids], buckets(:writebook).id assert_includes filter.buckets, buckets(:writebook) + users(:david).filters.create! tag_ids: [ tags(:mobile).id, tags(:web).id ], bucket_ids: [ buckets(:writebook).id ] + assert_difference "Filter.count", -1 do + tags(:web).destroy! + end + assert_changes "filter.reload.updated_at" do tags(:mobile).destroy! end