title: How to add or update an associated attribute while creating a new user at sign up with Devise?
body: <p>I'm having three models:</p>
  
  <ul>
  <li>Users can have several Interests and several Places.</li>
  <li>Users can share same Interests, but the Interests table should not
  contain duplicates and a user should not have duplicates Interests
  too.</li>
  <li>A Place can have several Users and can have Interests through its
  Users.</li>
  </ul>
  
  <p>I would like to be able to create a user at sign up and add its interests and places as associated attributes.</p>
  
  <p>Users can share same interests, so if it an interest already exist at sign up, I would like to simply add a link for the new user.</p>
  
  <p>What I could get until now is to add interests while signing up with the user, but if an interest already exist, than I get an error and the user is not registered at all.
  This is because I have a put an index on the interests and added a uniquesness validation on interests. Without it it was adding duplicate interests in the interests table.</p>
  
  <p>Here are the classes I use:</p>
  
  <p><code>models/user.rb</code></p>
  
  <pre><code>class User &lt; ApplicationRecord
    # Include default devise modules. Others available are:
    # :confirmable, :lockable, :timeoutable and :omniauthable
    devise :database_authenticatable, :registerable,
           :recoverable, :rememberable, :confirmable, :trackable, :validatable
  
    has_and_belongs_to_many :interests
    has_and_belongs_to_many :places
  
    validates :username, uniqueness: { case_sensitive: false }
    validates_associated :interests
  
    accepts_nested_attributes_for :interests
  
    def interests_list=value
      value.split(',').each do |interest|
        self.interests.build(:name =&gt; interest).save
      end
    end
  
    def interests_list
      self.interests.join(',')
    end
  end
  </code></pre>
  
  <p><code>models/place.rb</code></p>
  
  <pre><code>class Place &lt; ApplicationRecord
    has_and_belongs_to_many :users
    has_many :interests, through: :users
  end
  </code></pre>
  
  <p><code>models/interest.rb</code></p>
  
  <pre><code>class Interest &lt; ApplicationRecord
    has_and_belongs_to_many :users
  
    validates :name, uniqueness: { case_sensitive: false }
  end
  </code></pre>
  
  <p>The form for Devise:</p>
  
  <pre><code>&lt;%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %&gt;
    &lt;h3&gt;Add interests&lt;/h3&gt;
  
    &lt;div class="field"&gt;
      &lt;%= f.label :interests %&gt;&lt;br /&gt;
      &lt;%= f.text_field :interests_list %&gt;
    &lt;/div&gt;
  
    &lt;% if !user_signed_in? %&gt;
      &lt;h3&gt;Sign up&lt;/h3&gt;
  
        &lt;%= devise_error_messages! %&gt;
  
        &lt;div class="field"&gt;
          &lt;%= f.label :email %&gt;&lt;br /&gt;
          &lt;%= f.email_field :email, autofocus: true, autocomplete: "email" %&gt;
        &lt;/div&gt;
  
        &lt;div class="field"&gt;
          &lt;%= f.label :username %&gt;&lt;br /&gt;
          &lt;%= f.text_field :username %&gt;
        &lt;/div&gt;
  
        &lt;div class="field"&gt;
          &lt;%= f.label :password %&gt;
          &lt;% if @minimum_password_length %&gt;
          &lt;em&gt;(&lt;%= @minimum_password_length %&gt; characters minimum)&lt;/em&gt;
          &lt;% end %&gt;&lt;br /&gt;
          &lt;%= f.password_field :password, autocomplete: "off" %&gt;
        &lt;/div&gt;
  
        &lt;div class="field"&gt;
          &lt;%= f.label :password_confirmation %&gt;&lt;br /&gt;
          &lt;%= f.password_field :password_confirmation, autocomplete: "off" %&gt;
        &lt;/div&gt;
    &lt;% end %&gt;
  
    &lt;div class="actions"&gt;
      &lt;%= f.submit "Sign up" %&gt;
    &lt;/div&gt;
  
  &lt;% end %&gt;
  </code></pre>
  
  <p>I'm using Rails 5.1.4 and Devise 4.4.0</p>