title: Can't figure out how to add polymorphic tags in a rails app
body: <p>I have a sample rails app with a list of users. And I wanted to experiment with polymorphic tags.</p>
  
  <p>At this moment I can create tags for users through console in the following way
  <code>User.first.tags.create(name: "new tag name")</code></p>
  
  <p>But have problems with adding them through webform</p>
  
  <p>Here's what I did:</p>
  
  <pre><code>rails g model Tag name taggable:references{polymorphic}
  </code></pre>
  
  <p>generated the following migration </p>
  
  <pre><code>class CreateTags &lt; ActiveRecord::Migration[5.1]
    def change
      create_table :tags do |t|
        t.string :name
        t.references :taggable, polymorphic: true
  
        t.timestamps
      end
    end
  end
  </code></pre>
  
  <p>tag model</p>
  
  <pre><code>class Tag &lt; ApplicationRecord
    belongs_to :taggable, polymorphic: true
  end
  </code></pre>
  
  <p>user model</p>
  
  <pre><code>class User &lt; ApplicationRecord
    has_many :tags, as: :taggable
  end
  </code></pre>
  
  <p>Tags field</p>
  
  <pre><code>&lt;%= form_with(model: user, local: true) do |form| %&gt;
  ...
    &lt;div class="field"&gt;
      &lt;%= form.label :tag_list %&gt;
      &lt;%= form.text_field :tag_list, placeholder: "tags separated by comma" %&gt;
    &lt;/div&gt;
  ...
  &lt;% end %&gt;
  </code></pre>
  
  <p>I also found the following code but getting <code>You cannot call create unless the parent is saved</code> error.</p>
  
  <p>added the following setter to user model and added <code>:tag_list</code> to strong params of user</p>
  
  <pre><code>  def tag_list=(vals)
      self.tags = vals.split(", ").each do |val|
        tags.where(name: val.strip).first_or_create!
      end
    end
  </code></pre>