diff --git a/app/models/tag_implication.rb b/app/models/tag_implication.rb index 9d9a851ad..b10a4c65a 100644 --- a/app/models/tag_implication.rb +++ b/app/models/tag_implication.rb @@ -77,7 +77,7 @@ class TagImplication < TagRelationship module ValidationMethods def absence_of_circular_relation # We don't want a -> b && b -> a chains - if TagImplication.active.exists?(["antecedent_name = ? and consequent_name = ?", consequent_name, antecedent_name]) + if descendants.include?(antecedent_name) errors[:base] << "Tag implication can not create a circular relation with another tag implication" end end diff --git a/test/unit/tag_implication_test.rb b/test/unit/tag_implication_test.rb index d1dd50880..d5352fcff 100644 --- a/test/unit/tag_implication_test.rb +++ b/test/unit/tag_implication_test.rb @@ -80,10 +80,13 @@ class TagImplicationTest < ActiveSupport::TestCase should "not validate when a circular relation is created" do ti1 = FactoryBot.create(:tag_implication, :antecedent_name => "aaa", :consequent_name => "bbb") - ti2 = FactoryBot.build(:tag_implication, :antecedent_name => "bbb", :consequent_name => "aaa") - ti2.save - assert(ti2.errors.any?, "Tag implication should not have validated.") - assert_equal("Tag implication can not create a circular relation with another tag implication", ti2.errors.full_messages.join("")) + ti2 = FactoryBot.create(:tag_implication, :antecedent_name => "bbb", :consequent_name => "ccc") + ti3 = FactoryBot.build(:tag_implication, :antecedent_name => "bbb", :consequent_name => "aaa") + + assert(ti1.valid?) + assert(ti2.valid?) + refute(ti3.valid?) + assert_equal("Tag implication can not create a circular relation with another tag implication", ti3.errors.full_messages.join("")) end should "not validate when a transitive relation is created" do