Method to add tags to microposts
This commit is contained in:
parent
b75f9277cc
commit
ad03410a65
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue