Method to add tags to microposts

This commit is contained in:
Trevor Vallender 2023-10-19 18:46:41 +01:00
parent b75f9277cc
commit ad03410a65
2 changed files with 13 additions and 0 deletions

View File

@ -10,4 +10,10 @@ class Micropost < ApplicationRecord
has_many :microposts_tags
has_many :tags, through: :microposts_tags
def add_tags(*tag_names)
tag_names.each do |tag|
tags << Tag.find_or_create_by(name: tag)
end
end
end

View File

@ -17,4 +17,11 @@ class MicropostTest < ActiveSupport::TestCase
@micropost.content = nil
assert_not @micropost.valid?
end
test "can add tags" do
assert_empty @micropost.tags
assert_difference "@micropost.tags.count", +3 do
@micropost.add_tags("foo", "bar", "baz")
end
end
end