I have a Rails application with two models: SalesTransactions and PurchaseOrders.
In the PurchaseOrders model, new records are registered using "purchase_order_number" as the key field. I use the model’s create method to search if this “purchase_order_number” was previously registered, and if so, reuse this entry and use your identifier in the SalesTransaction entry. If this name has not been registered yet, I proceed and create, and then use the new PurchaseOrder record identifier in SalesTransaction (foreign_id link to the associated PO).
Please note that I do not have an existing PurchaseOrder record identifier until I have searched in the create method (so this is not a question “how do I update a record using“ accepts_nested_attributes_for "?”, I can do this as soon as I have the ID )
In some situations, my application registers a new SalesTransaction and simultaneously creates a new PurchaseOrder. It uses accepts_nested_attributes_for to create a PurchaseOrder record.
The problem is that when using "accepts_nested_attributes_for" creation is not called, so my model is not able to intercept the creation and search if "purchase_order_number" is already registered and handles this case.
I would appreciate advice on how to intercept the "accepts_nested_attributes_for" creations to allow some preprocessing (that is, look if the PurchaseOrder entry with this number already exists, and if so, use it).
Not all Sales have a PurchaseOrder, so a PurchaseOrder entry is optional in SalesTransaction.
(I saw kludge involving: reject_if, but this does not allow me to add the existing record identifier as foreign_id in the parent record.)
Thanks.
Snips source share