From 41923faf2ee155938a0b4636e94cb2feb6bfa9a8 Mon Sep 17 00:00:00 2001 From: Olivier Bellone Date: Thu, 12 Oct 2023 12:52:54 -0600 Subject: [PATCH] Add the update_only option to accepts_nested_attributes_for attributes writers --- README.adoc | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/README.adoc b/README.adoc index 9a71bea..b8ee633 100644 --- a/README.adoc +++ b/README.adoc @@ -814,6 +814,25 @@ class Post < ApplicationRecord end ---- +=== `accepts_nested_attributes_for` Update Only Option [[accepts_nested_attributes_for-update_only-option]] + +Define the `update_only` option to the `accepts_nested_attributes_for` attributes writers. + +[source,ruby] +---- +# bad +class Member < ApplicationRecord + has_one :avatar + accepts_nested_attributes_for :avatar +end + +# good +class Member < ApplicationRecord + has_one :avatar + accepts_nested_attributes_for :avatar, update_only: true +end +---- + === `save!` [[save-bang]] When persisting AR objects always use the exception raising bang! method or handle the method return value.