2023-09-17 11:45:24 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class Micropost < ApplicationRecord
|
|
|
|
belongs_to :user
|
|
|
|
has_rich_text :content
|
|
|
|
|
2023-10-05 15:46:07 +00:00
|
|
|
validates :user,
|
|
|
|
:content,
|
|
|
|
presence: true
|
2023-10-19 17:35:48 +00:00
|
|
|
|
|
|
|
has_many :microposts_tags
|
|
|
|
has_many :tags, through: :microposts_tags
|
2023-10-19 17:46:41 +00:00
|
|
|
|
2023-10-19 18:36:08 +00:00
|
|
|
def set_tags(*tag_names)
|
|
|
|
tag_names.each do |tag_name|
|
|
|
|
tag = Tag.find_or_create_by(name: tag_name)
|
|
|
|
tags << tag unless tags.include?(tag)
|
2023-10-19 17:46:41 +00:00
|
|
|
end
|
|
|
|
end
|
2023-09-17 11:45:24 +00:00
|
|
|
end
|