How to update related models with accepts_nested_attributes_for

Thing you want to do

Using accepts_nested_attributes_for, we are also creating and updating related models at the same time.

There was a case where I wanted to update only the related model at the timing of creation.

How to do

https://github.com/rails/rails/issues/7256#issuecomment-93172189 It is a memo that I chewed for myself.

Foo (existing update)-> Bar (create)-> Baz (existing update) ・ Foo is updated ・ Create Bar ・ Baz updated I want to

In that case, just pass params as follows.

{
  foo: {
    id: "foo_id", =>Pass id to update foo
    ...
    bar_attributes: {
      ...
      baz_id: "baz_id", =>baz to update baz_id and
      baz_attributes: {
        id: "baz_id",  =>Pass the id.
        ...
      }
    }
}

Recommended Posts

How to update related models with accepts_nested_attributes_for
How to update with activerecord-import
How to number (number) with html.erb
How to get started with slim
[Java] How to update Java on Windows
How to enclose any character with "~"
How to get along with Rails
Update MySQL from 5.7 to 8.0 with Docker
How to start Camunda with Docker
How to crop an image with libGDX
How to adjustTextPosition with iOS Keyboard Extension
How to compile Java with VsCode & Ant
[Java] How to compare with equals method
[Android] How to deal with dark themes
How to use BootStrap with Play Framework
[Rails] How to use rails console with docker
[Note] How to get started with Rspec
How to do API-based control with cancancan
How to achieve file download with Feign
How to set JAVA_HOME with Maven appassembler-maven-plugin
How to implement TextInputLayout with validation function
How to handle sign-in errors with devise
How to delete data with foreign key
How to test private scope with JUnit
How to monitor nginx with docker-compose with datadog
How to deal with Precompiling assets failed.
How to achieve file upload with Feign
How to run Blazor (C #) with Docker
How to build Rails 6 environment with Docker
How to download Oracle JDK 8 rpm with curl
[Java] How to test for null with JUnit
How to mock each case with Mockito 1x
How to mock each case with PowerMock + Mockito1x
How to test interrupts during Thread.sleep with JUnit
How to use built-in h2db with spring boot
How to search multiple columns with gem ransack
How to deploy
[Swift] How to link the app with Firebase
How to create multiple pull-down menus with ActiveHash
How to update pre-built files in docker container
How to get started with Eclipse Micro Profile
How to create search conditions involving multiple models
How to give your image to someone with docker
How to insert all at once with MyBatis
How to monitor SPA site transitions with WKWebView
How to write test code with Basic authentication
[Rails] How to easily implement numbers with pull-down
How to build API with GraphQL and Rails
How to use nfs protocol version 2 with ubuntu 18.04
How to use docker compose with NVIDIA Jetson
How to get resource files out with spring-boot
How to create member variables with JPA Model
How to verify variable items with WireMock's RequestBodyMatching
How to use nginx-ingress-controller with Docker for Mac
[Rails] How to build an environment with Docker
How to avoid exceptions with Java's equals method
How to redirect after user login with Spring-security
[Rails] How to search across columns of related models (parent or child) in ransack
[Docker] How to update using a container on Heroku and how to deal with Migrate Error
How to embed JavaScript variables in HTML with Thymeleaf
How to implement UICollectionView in Swift with code only