Closed CarlosEMedrano27 closed 4 years ago
@CarlosEMedrano27 which reform's version are you using? and can you please share the complete reform object?
Hi @emaglio! Thanks for your help
module Contracts
class RequestStudentSpecialDocumentsContract < Reform::Form
collection :student_special_documents, model_identifier: :id, form: Contracts::RequestStudentSpecialDocumentContract, populator: :populator!
def populator!(fragment:, **)
item = student_special_documents.find { |document| document.id.to_s == fragment["id"].to_s }
if fragment["delete"] == "1"
student_special_documents.delete(item)
return skip!
end
item || student_special_documents.append(StudentSpecialDocument.new)
end
end
end
I'm using the reform (2.2.4)
@CarlosEMedrano27 just added a test case really similar to yours for v2.3 and it seems populate correctly (https://github.com/trailblazer/reform/commit/df8cb725dcf84b159480364dacba1efc98178b76) - please update reform version and try again. Thanks
Complete Description of Issue
using populator on collection is only saving one new record, I send the following JSON:
"student_special_documents": [ { "document_type_id": 2, "document_uploader": { "first_name": "Carlos", "last_name": "Medrano" }, "college_ids": [5,9,8], "uploaded_at": "" }, { "document_type_id": 1, "document_uploader": { "first_name": "Carlos", "last_name": "Medrano" }, "college_ids": [5,9,8] } ]
and it's only saving the last oneSteps to reproduce
I created a form with a collection and the following method being called on the populator
def populator!(fragment:, **) item = student_special_documents.find { |document| document.id.to_s == fragment["id"].to_s } if fragment["delete"] == "1" student_special_documents.delete(item) return skip! end item ? item : student_special_documents.append(StudentSpecialDocument.new) end
Expected behavior
It should be adding 2 new records to the relation
Actual behavior
It's adding just the last one