I have assigned record like this
profile_image = current_user.profile_images.build(params_profile_image)
But I don't want to save this record any more. Now how can I delete this profile_image unsaved record ?
I have assigned record like this
profile_image = current_user.profile_images.build(params_profile_image)
But I don't want to save this record any more. Now how can I delete this profile_image unsaved record ?
For the variable
profile_image, you don't need to do anything, it will be deleted once your code exit the block of where the variable is declared.If you don't want to do anything else with
current_userthen you can just leave it too, and after the code exit the block where the variable is declared it will be gone, and next time you retrievecurrent_userit will be a new copy without the unsaved changes.However if you want to continue using
current_useryou can docurrent_user.restore_attributesthis will revert all unsaved changes tocurrent_user. If you have other unsaved changes that you need to keep. I would recommend you to restructure the code to not callcurrent_user.profile_images.build(params_profile_image)but ratherFoo.build(params_profile_image)whereFoois the model class forprofile_images. And only assigncurrent_user.profile_images=profile_imageonce you're sure you need to save.