title: Set user_id for nested_attribute in devise registration before user exists
body: <p>The idea is simple.  I require a nested attribute <code>tag</code> for user registration.  <code>tag</code> requires a <code>user_id</code>.</p>
  
  <p><strong>the view</strong></p>
  
  <pre><code>&lt;%= form_for(resource, as: resource_name, url: registration_path(resource_name), html: {role: :form}) do |f| %&gt;
    &lt;%= f.fields_for :tags, resource.tags.build do |a| %&gt;
      &lt;%= a.text_field :tagname %&gt;
    &lt;% end %&gt;
  &lt;% end %&gt;
  </code></pre>
  
  <p><strong>tag migration</strong></p>
  
  <pre><code>class CreateTags &lt; ActiveRecord::Migration
    def change
      create_table :tags do |t|
        t.string :tagname
        t.references :user, index: true
  
        t.timestamps
      end
      add_index :tags, :tagname, unique: true
    end
  end
  </code></pre>
  
  <p><strong>the tag model</strong></p>
  
  <pre><code>class Tag &lt; ActiveRecord::Base
    belongs_to :user
    validates_presence_of :user_id
    validates_uniqueness_of :tagname
  end
  </code></pre>
  
  <p><strong>the user model</strong></p>
  
  <pre><code>class User &lt; ActiveRecord::Base
    has_many :tags, autosave: true, dependent: :destroy
    accepts_nested_attributes_for :tags
  end
  </code></pre>
  
  <p><strong>strong parameters</strong></p>
  
  <pre><code>def configure_permitted_parameters
    devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(
            :tags_attributes =&gt; [:id, :user_id, :tagname]
          ) }
  </code></pre>
  
  <p>The save feature is untouched at the moment.  The form passes the nested attribute :tagname just fine.  But I've been unable to get the "would be" <code>user_id</code> from <code>resource</code>.</p>
  
  <p>I've already looked for hours online for any answer to this.  None has appeared, but the idea that the nested attribute should be saved after the initial user object is saved sounds like a workable solution.  But then it's no longer handled as a nested attribute.</p>
  
  <p>Help is appreciated!  Thanks!</p>