title: Rails Nested Attributes Not Saving In A One To One Relation (API Only)
body: <p>For some reason, none of my one-to-one relations are being saved in rails when using the <code>accepts_nested_attributes_for</code></p>
  
  <p>Take example of this, there is a <code>User</code> table and a <code>Profile</code> table. There's a direct relationship between them and each user is required to have a profile</p>
  
  <p>Therefore on <code>POST /users/</code> it's necessary to create the associated profile for the user.</p>
  
  <p>However, for some reason that does not happen and I get a NULL column violation error as all of my nested attributes are being ignored despite being permitted.</p>
  
  <p>My user model is like this:</p>
  
  <pre class="lang-rb prettyprint-override"><code>class User &lt; ApplicationRecord
    authenticates_with_sorcery!
  
    attr_writer :profile_attributes
  
    has_many :accounts, foreign_key: 'users_id'
    has_one :profile, foreign_key: 'user_id'
    has_many :authentications, :dependent =&gt; :destroy
  
    ...
  
    validates :password, password: true, on: :create
    validates :email, presence: true, uniqueness: true, format: { with: URI::MailTo::EMAIL_REGEXP } 
  
    before_create :set_user_type
    before_save :build_profile
  
    accepts_nested_attributes_for :profile, reject_if: :all_blank
    accepts_nested_attributes_for :authentications
  
    ...
  end
  </code></pre>
  
  <p>With the profile model like this:</p>
  
  <pre class="lang-rb prettyprint-override"><code>class Profile &lt; ApplicationRecord
    belongs_to :user, foreign_key: 'user_id', polymorphic: true, optional: true
  
    attr_accessor :first_name, :last_name
  
    ...
  
    validates_date :birthday, before: -&gt; { 16.years.ago }, before_message: 'must be at least 16 years old', allow_nil: true
  
    validates :profile_image, url: { allow_nil: true, no_local: true }
    validates :first_name, presence: true
    validates :last_name, presence: true
    validates :gender, enum: genders
  
    ...
  end
  
  </code></pre>
  
  <p>And the UsersController (create method)</p>
  
  <pre class="lang-rb prettyprint-override"><code> def create
      user = User.create user_params
  
      raise RBError::ValidationError.new(user.errors) unless user.save
      respond user.as_json, :created
    end
  
  private
    def user_params
      params.require(:data)
        .permit(:email, :password, :phone_number, {
          profile_attributes: [
            :first_name, :last_name, :gender, :dob, :profile_image
          ]
        })
    end
  </code></pre>
  
  <p>The JSON request is like so:</p>
  
  <pre><code>{
      "data": {
          "email": [omitted],
          "password": [omitted],
          "profile_attributes": {
              "first_name": "John",
              "last_name": "Doe"
          }
      }
  }
  </code></pre>
  
  <p>With this response error</p>
  
  <pre><code> "#&lt;ActiveRecord::NotNullViolation: PG::NotNullViolation: ERROR:  null value in column \"first_name\" violates not-null constraint\nDETAIL:  Failing row contains (23, 31, null, null, null, null, 2019-08-10 11:49:40.021683, 2019-08-10 11:49:40.021683, null, null).\n: INSERT INTO \"profiles\" (\"user_id\", \"created_at\", \"updated_at\") VALUES ($1, $2, $3) RETURNING \"id\"&gt;",
  </code></pre>
  
  <p>So as you can see ALL of the attributes sent in the request are being completely ignored. I have no idea why and it's been driving my crazy. I can't seem to find anything remotely similar to the issue I'm having.</p>
  
  <p>All of the parameters are being permitted too and the validations on the profile model work as expected.</p>
  
  <pre><code> Parameters: {"data"=&gt;{"email"=&gt;"sample@fgmail.com", "password"=&gt;"[FILTERED]", "profile_attributes"=&gt;{"first_name"=&gt;"John", "last_name"=&gt;"Doe"}}, "user"=&gt;{}}
  &lt;ActionController::Parameters {"email"=&gt;"sample@fgmail.com", "password"=&gt;[OMITTED], "profile_attributes"=&gt;&lt;ActionController::Parameters {"first_name"=&gt;"John", "last_name"=&gt;"Doe"} permitted: true&gt;} permitted: true&gt;
  </code></pre>