我有以下模型及其相应的架构定义:
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
正如您从上面的评论中看到的,id
不是为Face
模型自动生成的,而是期望在创建它们时输入(因为我们正在建模现实世界中已经具有唯一标识符的东西,我们想要使用而不是添加另一个 ID)。
当我尝试执行以下操作时,问题就来了:
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=
这对我来说意味着,由于我们正在传递一个id
,Rails 期望这是对关联的更新Face
,而不是传递一个Face
要创建并与cube
.
有没有办法让我禁用此默认行为accepts_nested_attributes_for
,或者我是否必须为我的特定用例编写自定义代码?