Rails validation and Rollback
I have the following code that gets run whe开发者_运维问答n validation from within a validation method.
def validate
if self.limit_reached = true
self.errors.add('plan', 'limit reached')
self.account_setting.update_attribute(:email_sent, true)
end
end
However as the validation is failing, this update is being rolled back, how can I prevent this one update from being rolled back
Try this:
def validate
if self.limit_reached
self.errors.add('plan', 'limit reached')
@set_email_sent = true
return false
end
end
def after_rollback
if @set_email_sent
self.account_setting.update_attribute(:email_sent, true)
end
end
Hope it helps!
精彩评论