postgresql - Rails before_destroy callback db changes always rolled back -
i'm trying prevent deletion of models db , pretty much follow guide (see 9.2.5.3 exercise paranoia before_destroy) rails 4 book.
i have simple model:
class user < activerecord::base before_destroy update_attribute(:deleted_at, time.current) false end
and in controller:
def destroy @user = user.find(params[:id]) # @user.update!(deleted_at: time.zone.now) # if here works @user.destroy # if comment line... render :show end
the callback gets called , attribute gets set, database transaction gets rolled back. leave out returning of false
model gets deleted because execution of delete not halted.
as can see in comments can work want utilize service object , set logic out of controller.
if callback returns false transaction rollbacked.
for want should not phone call destroy
method on arel object. instead, create own method soft_destroy
or , update attribute.
and prevent others calling destroy
method on arel object, add together callback raising , exception instance.
ruby-on-rails postgresql callback rails-activerecord activemodel
No comments:
Post a Comment