diff --git a/app/models/tag_alias.rb b/app/models/tag_alias.rb index 58c36721e..b5f973fae 100644 --- a/app/models/tag_alias.rb +++ b/app/models/tag_alias.rb @@ -1,5 +1,4 @@ class TagAlias < TagRelationship - before_save :ensure_tags_exist after_save :clear_all_cache after_destroy :clear_all_cache after_save :clear_all_cache, if: ->(rec) {rec.is_retired?} @@ -145,11 +144,6 @@ class TagAlias < TagRelationship end end - def ensure_tags_exist - Tag.find_or_create_by_name(antecedent_name) - Tag.find_or_create_by_name(consequent_name) - end - def ensure_category_consistency if antecedent_tag.category != consequent_tag.category && antecedent_tag.category != Tag.categories.general consequent_tag.update_attribute(:category, antecedent_tag.category) diff --git a/app/models/tag_relationship.rb b/app/models/tag_relationship.rb index deb39def8..a33e045f2 100644 --- a/app/models/tag_relationship.rb +++ b/app/models/tag_relationship.rb @@ -10,8 +10,8 @@ class TagRelationship < ApplicationRecord belongs_to :approver, class_name: "User", optional: true belongs_to :forum_post, optional: true belongs_to :forum_topic, optional: true - has_one :antecedent_tag, :class_name => "Tag", :foreign_key => "name", :primary_key => "antecedent_name" - has_one :consequent_tag, :class_name => "Tag", :foreign_key => "name", :primary_key => "consequent_name" + belongs_to :antecedent_tag, class_name: "Tag", foreign_key: "antecedent_name", primary_key: "name", default: -> { Tag.find_or_create_by_name(antecedent_name) } + belongs_to :consequent_tag, class_name: "Tag", foreign_key: "consequent_name", primary_key: "name", default: -> { Tag.find_or_create_by_name(consequent_name) } has_one :antecedent_wiki, through: :antecedent_tag, source: :wiki_page has_one :consequent_wiki, through: :consequent_tag, source: :wiki_page diff --git a/test/unit/tag_implication_test.rb b/test/unit/tag_implication_test.rb index aac6b0904..d1dd50880 100644 --- a/test/unit/tag_implication_test.rb +++ b/test/unit/tag_implication_test.rb @@ -64,6 +64,13 @@ class TagImplicationTest < ActiveSupport::TestCase assert_equal(CurrentUser.user.id, ti.creator_id) end + should "ensure both tags exist" do + FactoryBot.create(:tag_implication, antecedent_name: "a", consequent_name: "b") + + assert(Tag.exists?(name: "a")) + assert(Tag.exists?(name: "b")) + end + should "not validate when a tag directly implicates itself" do ti = FactoryBot.build(:tag_implication, antecedent_name: "a", consequent_name: "a")