title: Is there a suggested way to create nested many-to-many entities in RoR that avoids *_attributes in the JSON field for the nested resource?
body: <p>I've managed to get nested many-to-many entities created through the use of <code>accepts_nested_attributes_for</code> for the JSON and the project below.</p>
  <p>My question is - is there a suggested way to achieve the same thing where I don't have to add <code>_attributes</code> to be able to create the array of <code>emotion</code> objects on the RoR server? What I mean by suggested is either something the framework itself suggests or otherwise something as clean as possible.</p>
  <p>JSON getting posted to http://localhost:3000/periods:</p>
  <pre><code>{
      &quot;period&quot;: {
          &quot;date&quot;: &quot;2022-03-17T03:00:52.820Z&quot;,
          &quot;period&quot;: &quot;morning&quot;,
          &quot;emotions_attributes&quot;: [
              {
                  &quot;name&quot;: &quot;ok&quot;
              },
              {
                  &quot;name&quot;: &quot;fine&quot;
              }
          ]
      }
  }
  </code></pre>
  <p><code>rails generate</code> commands used to create subsequent files:</p>
  <pre><code>rails g model Emotion name:string
  rails g scaffold Period date:date period:string --skip-template-engine
  rails g scaffold Entry emotion:references period:references --skip-template-engine
  </code></pre>
  <p><strong>periods.rb</strong>:</p>
  <pre><code>class Period &lt; ApplicationRecord
    has_many :entries
    has_many :emotions, through: :entries
  
    accepts_nested_attributes_for :emotions
  end
  </code></pre>
  <p><strong>emotion.rb</strong></p>
  <pre><code>class Emotion &lt; ApplicationRecord
    has_many :entries
    has_many :periods, through: :entries
  end
  </code></pre>
  <p><strong>entry.rb</strong></p>
  <pre><code>class Entry &lt; ApplicationRecord
    belongs_to :emotion
    belongs_to :period
  end
  </code></pre>
  <p><strong>periods_controller.rb</strong></p>
  <pre><code>class PeriodsController &lt; ApplicationController
    before_action :set_period, only: %i[ show edit update destroy ]
    skip_before_action :verify_authenticity_token
  
    ...
  
    def create
      @period = Period.new(period_params)
  
      respond_to do |format|
        if @period.save
          format.html { redirect_to period_url(@period), notice: &quot;Period was successfully created.&quot; }
          format.json { render :show, status: :created, location: @period }
        else
          format.html { render :new, status: :unprocessable_entity }
          format.json { render json: @period.errors, status: :unprocessable_entity }
        end
      end
    end
    
    ...
  
      # FYI turns out the id is key here to be able to even perform create 
      def period_params
        params.require(:period).permit(
          :date, 
          :period,
          emotions_attributes: [:id, :name]
        )
      end
  </code></pre>
  <p><strong>routes.rb</strong></p>
  <pre><code>Rails.application.routes.draw do
    resources :entries
    resources :periods
  </code></pre>
  <p>What I'd like to be able to post is this:</p>
  <pre><code>{
      &quot;period&quot;: {
          &quot;date&quot;: &quot;2022-03-17T03:00:52.820Z&quot;,
          &quot;period&quot;: &quot;morning&quot;,
          &quot;emotions&quot;: [
              {
                  &quot;name&quot;: &quot;ok&quot;
              },
              {
                  &quot;name&quot;: &quot;fine&quot;
              }
          ]
      }
  }
  </code></pre>