Scott
Scott

Reputation: 1127

Rails validation and Rollback

I have the following code that gets run when 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

Upvotes: 1

Views: 1451

Answers (1)

thomasfedb
thomasfedb

Reputation: 5983

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!

Upvotes: 3

Related Questions