Hi guys,
I was trying to create a record in both the parent and child tables (1 to 1
relationship). By default when you call parent.save it''s not invoked in
a
transaction mode, and I''m trying to avoid parent record is created
without
the child record (due to validation error in child).
I wanted to save both parent and child in transaction mode:
parent = Parent.new
child = parent.build_child
Parent.transaction do
parent.save! # parent created, child validation check failed but
no exception thrown
child.save! # exception triggered for validation error, transaction
rolled back
end
The problem is because parent was initially successfully created,
paret.new_record? returns true, even though it was rolled back later on, so
subsequent attempt to save will not insert the parent record.
Am I missing something here? Please help to shed some lights? Thanks!
Regards,
Herry