Home>
I want to solve

I want to display validation when the button is pressed on the edit screen.
I'm trying to save to DB with assign_attributes in the action.
(Validation before transition to the edit confirmation screen)

Source
module Api
  module V1
    class SuAccountsController</pre>
<p><br />
The parameter is POSTed to the "update_confirm" action in the API.</p>
<pre><code data-language = "params">app/controllers/api/v1/su_accounts_controller.rb: 14: in `update_confirm '
Started POST "/ api/v1/su_accounts/update_confirm" for 127.0.0.1 at 2019-12-05 12:36:02 +0000
Cannot render console from 172.18.0.1! Allowed networks: 127.0.0.0/127.255.255.255, :: 1
Processing by Api :: V1 :: SuAccountsController # update_confirm as JSON
  Parameters: {"su_account" =>{"id" =>1, "group_name" =>"good department", "image_url" =>"", "su_company_attributes" =>{"su_account_id" =>1, "company_name" =>"", "company_hp" =>"https://hoge.com", "establishment_year" =>2019, "establishment_month" =>11, "establishment_date" =>1, "representative" =>"Taro Hoge" , "business_description" =>"Development of Hogehoge", "capital" =>2000000, "capital_unit" =>"us_dollar", "address" =>"Huge City, Hoge" ",

 "user_attributes" =>{"email" =>"[email protected]", "last_name" =>"Hagetaro", "first_name" =>"Hagematsu", "tel" =>nil}}}


And in@ su_account.assign_attributes (update_su_account_params)in the action

SuAccount Load (1.1ms) SELECT `su_accounts`. * FROM` su_accounts` WHERE `su_accounts`.`deleted_at` IS NULL AND` su_accounts`.`id` = 1 LIMIT 1
  ↳ app/controllers/api/v1/su_accounts_controller.rb: 31: in `set_su_account '
  SuCompanyInfo Load (0.6ms) SELECT `su_company_infos`. * FROM` su_company_infos` WHERE `su_company_infos`.`deleted_at` IS NULL AND` su_company_infos`.`su_account_id` = 1 LIMIT 1
  ↳ app/controllers/api/v1/su_accounts_controller.rb: 14: in `update_confirm '
   (0.4ms) BEGIN
  ↳ app/controllers/api/v1/su_accounts_controller.rb: 14: in `update_confirm '
  SuCompanyInfo Update (1.6ms) UPDATE `su_company_infos` SET` su_company_infos`.`su_account_id` = NULL, `su_company_infos`.`updated_at` = '2019-12-05 12: 36: 02.686532' WHERE` su_company_infos`.`id` = 1
  ↳ app/controllers/api/v1/su_accounts_controller.rb: 14: in `update_confirm '
   (0.6ms) ROLLBACK
  ↳ app/controllers/api/v1/su_accounts_controller.rb: 14: in `update_confirm '
Completed 500 Internal Server Error in 112ms (ActiveRecord: 12.3ms | Allocations: 26144)

ActiveRecord :: NotNullViolation (Mysql2 :: Error: Column 'su_account_id' cannot be null):
app/controllers/api/v1/su_accounts_controller.rb: 14: in `update_confirm '


I am trying to save it.
Columns that can't be saved because they are null are columns that you don't want to update. I don't think assign_attributes should be saved in the first place.

Pass parameters to

@su_account, validate without saving,
I want to receive error contents in json.

Tell us if you can tell why it is stored.
Thank you.

* Strong parametersu_account_idhas the same result whether or not it exists.

Added model.
I feel the relationship is suspicious. .

class SuAccount</pre>
<pre><code>class SuCompanyInfo  {establishment_month.presence || establishment_date.presence}
  validates: establishment_month,
    presence: true, if:->{establishment_year.presence}
  validates: establishment_date,
    presence: true, if:->{establishment_year.presence}
  # Representative
  validates: representative,
    length: {maximum: 50}# Business description
  validates: business_description,
    length: {maximum: 500}
  # Capital
  validates: capital,
    length: {maximum: 20},


    numericality: {
      only_integer: true,
      allow_blank: true
    }
  # Currency unit
  validates: capital_unit,
    presence: true, if:->{capital.presence}
  # Location
  validates: address, length: {maximum: 200}

  ##
  # enums
  ##
  enum capital_unit: {
    us_dollar: 1,
    yen: 2,
    yuan: 3
  }

  attribute: establishment_year
  attribute: establishment_month
  attribute: establishment_date
end
class User
  • Answer # 1

    With model relations

    accepts_nested_attributes_for: su_company, allow_destroy: true, update_only: true
      accepts_nested_attributes_for: user, allow_destroy: true, update_only: true


    Solved by addingupdate_only: truelike

    Referenced articles
    When accepting has_one associations_accepts_nested_attributes_for and assigning with attributes, it is destroyed if there is no id