title: Saving a new child to an existing parent using nested attributes
body: <p>So, I'm using nested attributes to create different childs using one create action, the first time it creates the parent everything works as it should, but when the parent is used again then there is a unique constraint error since it is trying to create a new parent record instead of using the old one to add the new child. Question is if this is working as intended and if it is, how could I fix the code so that I don't have to check for each association to create the record</p>
  <p>View</p>
  <pre><code>&lt;%= form_with model: @email, method: :post do |t| %&gt;
    &lt;%= t.text_field  :name, placeholder: 'Nombre'%&gt;
    &lt;%= t.text_field  :email, placeholder: 'Correo'%&gt;
    &lt;%= t.text_field  :phone, placeholder: 'Celular'%&gt;
    &lt;%= t.fields_for :credit_emails do |credit| %&gt;
    &lt;%= credit.text_field :amount, placeholder: '¿Cuánto necesitas?' %&gt;
    &lt;%= credit.text_field :pay_time, placeholder: '¿En cuánto puedes pagar?' %&gt;
    &lt;% end %&gt;
    &lt;%= t.fields_for :card_emails do |card| %&gt;
      &lt;%= card.text_field :content, placeholder: '¿Qué es lo que más te llamó la atención de la tarjeta?' %&gt;
    &lt;% end %&gt;
    &lt;%= t.fields_for :contact_emails do |contact| %&gt;
      &lt;%= contact.text_field :subject, placeholder: 'Asunto' %&gt;
      &lt;%= contact.text_field :content, placeholder: 'Escríbenos tu mensaje' %&gt;
    &lt;% end %&gt;
    &lt;%= t.submit 'Envío' %&gt;
  &lt;% end %&gt;
  </code></pre>
  <p>Controller</p>
  <pre class="lang-rb prettyprint-override"><code>class EmailsController &lt; ApplicationController
    def create
      @email = Email.create(email_params)
    end
  
    def email_params
      params.require(:email).permit(:name, :email, :phone, 
        contact_emails_attributes: %i[content subject],
        credit_emails_attributes: %i[amount pay_time], 
        card_emails_attributes: %i[content])
    end
  end
  
  </code></pre>
  <pre class="lang-rb prettyprint-override"><code>class StaticController &lt; ApplicationController
    before_action :email_create, except: %i[new simulator]
  
    def new
      @title = 'homepage'
    end
  
    def credit
      @email.credit_emails.build
    end
  
    def card
      @email.card_emails.build
    end
  
    def contact_page
      @email.contact_emails.build
    end
  
    def simulator; end
  end
  </code></pre>
  <p>Model</p>
  <pre class="lang-rb prettyprint-override"><code>class Email &lt; ApplicationRecord
    has_many :card_emails, foreign_key: :email_id, inverse_of: :email, autosave: true
    has_many :contact_emails, foreign_key: :email_id, inverse_of: :email, autosave: true
    has_many :credit_emails, foreign_key: :email_id, inverse_of: :email, autosave: true
  
    accepts_nested_attributes_for :card_emails
    accepts_nested_attributes_for :contact_emails
    accepts_nested_attributes_for :credit_emails
  
    before_save :downcase_email
  
    VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
    validates :name, presence: true, length: { minimum: 3, maximum: 20 }
    validates :email, presence: true, format: { with: VALID_EMAIL_REGEX }, uniqueness: { case_sensitive: false }
    validates :phone, presence: true, format: { with: /\d[0-9]+/ }, length: { minimum: 5, maximum: 10 }
  
    def downcase_email
      email.downcase!
    end
  end
  
  </code></pre>
  <p>Thank you for your help and I hope this helps other people</p>