Existing records disappear when building a model of has_one (rails)

class Parent
    has_one :child
end
class Child
    belongs_to :parent
end

In this situation, use parent.build_child to build child from the existing parent.

Normally, I thought that the DB record would not be rewritten just by building, but if you run this build when parent.child already exists, the original child will be Note that it will be built after being DELETEd

Postscript

In the first place, when an existing association exists, I don't think that build will form an action to run, but when the DB is directly rewritten and the child model has already been created, there is no destroy. This is a phenomenon that I noticed when the record disappeared.

Because it was a story that did not get caught even if I googled

reference

Recommended Posts

Existing records disappear when building a model of has_one (rails)
[Rails] Avoid SELECT issued when creating a model with belongs_to defined!
A note of someone who stumbled when trying to create a Rails project
A collection of methods often used when manipulating time with TimeWithZone of Rails
[Rails 6.0] About batch saving of multiple records
Rails Basics of creating a new application
A simple example of an MVC model
A memorandum when building an environment with Ruby3.0 x Rails6.1 x Docker x CentOS Stream