I have the following models with their corresponding schema definitions:
class Cube < ApplicationRecord
has_many :faces, inverse_of: :cube, dependent: :destroy
accepts_nested_attributes_for :faces
end
create_table "cubes", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
##
# NOTE: Even though this model's related database table has an +id+ column, it
# is not a regular auto-generated, auto-incremented ID, but rather an +id+
# column that refers to an uuid that must be manually entered for each
# Face that is created.
##
class Face < ApplicationRecord
belongs_to :cube, inverse_of: :faces, optional: true
validates :id, presence: true, uniqueness: true
end
# Note that id defaults to nil.
create_table "faces", id: :uuid, default: nil, force: :cascade do |t|
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.uuid "cube_id"
t.index ["cube_id"], name: "index_faces_on_cube_id"
end
As you can see from the comments above, id
is not automatically generated for the Face
model, but rather is expected to be entered when they are created (because we are modeling something that in the real world already has a unique identifier, which we want to use instead of adding another ID).
The problem comes when I try to do the following:
cube = Cube.new
cube.faces_attributes = [
{
id: "2bfc830b-fd42-43b9-a68e-b1d98e4c99e8"
}
]
# Throws the following error
# ActiveRecord::RecordNotFound: Couldn't find Face with ID=2bfc830b-fd42-43b9-a68e-b1d98e4c99e8 for Cube with ID=
which to me means that since we are passing an id
, Rails expects this to be an update on an associated Face
, instead of passing a new Face
to be created and associated with the cube
.
Is there a way for me to disable this default behavior by accepts_nested_attributes_for
, or do I have to write custom code for my specific use case?