I have two models:
class Topic < ActiveRecord::Base
has_and_belongs_to_many :tags
end
class Tag < ActiveRecord::Base
has_and_belongs_to_many :topics
end
I make a model for create them both.
class Forms::NewTopic
include ActiveModel::Model
attr_accessor :title, :user_id, :raw_link, :description, :tags
def save
topic_tags = []
@tags.gsub(/\s+/m, ' ').strip.split(" ").uniq.each do |tag_name|
tag = Tag.find_or_create_by name: tag_name
topic_tags << tag if tag.errors.blank?
break if topic_tags.count == 3
end
Topic.create title: @title,
user_id: @user_id,
raw_link: @raw_link,
description: @description,
tags: topic_tags
end
end
But when I want to update them, I cannot find some better solution.
Is there a better solution to solve the scene? Thx :)