开发者

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!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜