Rails: creating nested attributes with manually set IDs

107 views Asked by At

I have a rails API that injects primary key IDs from the client, as opposed to autogenerating through Rails. Specifically:

class ParentModel < ApplicationRecord
  accepts_nested_attributes_for: child_models
  has_many :child_models
end

class ChildModel < ApplicationRecord
  belongs_to :parent_model
end

Nested data is created via:

@parent_object = ParentModel.find_or_initialize_by(id: parent_model_params[:id])
@parent_object.assign_attributes(parent_model_params)
@parent_object.save

If the ID for a child_object already exists in the database, the operation updates the child object as expected. However, if the child object is new, I get:

Couldnt find ChildModel with ID=b35e8f02... for ParentModel with ID=0c9b60f3...

In short: I'm trying to ensure rails creates child records (with the given IDs) when they don't exist, and continues to update existing child records if they do. Any thoughts on how to do that through nested attributes?

1

There are 1 answers

0
PlankTon On

For those wondering, I couldn't find an elegant solution. Instead, I manually created a new child object for each new child before calling .assign_attributes. Eg:

parent_model_params[:child_model_attributes].each do |child_params|
  next if @parent_object.child_ids.include?(child_params[:id])

  @parent_object.child_objects << ChildModel.new(child_params)
end

@parent_object.assign_attributes(parent_model_params) # This no longer raises a RecordNotFound error