Liz
Liz

Reputation: 1445

Rails Stack Level Too Deep With after_update method

I'm trying to make it so that if a user changes either of two boolean variables (buyer and seller, they can be changed in either direction to trigger the action) it will reset the status of a third variable status_confirmed to false.

I have the following in my user model:

after_update :reset_confirmed

  def reset_confirmed
    if self.buyer_changed? || self.seller_changed?
      self.update_attributes(status_confirmed: false)
    end
  end

From what I can understand stack level too deep errors are due to infinite loops or recursions, which I can't find. Can anyone see where I'm going wrong?

Upvotes: 3

Views: 1234

Answers (1)

Md. Farhan Memon
Md. Farhan Memon

Reputation: 6121

2 ways to handle this:

after_update :reset_confirmed
def reset_confirmed
  self.update_column(:status_confirmed, false) if self.buyer_changed? || self.seller_changed?
end

The difference between update_attribute and update_column is what will help you, as the latter skips callbacks if any.

OR

before_save  :reset_confirmed
def reset_confirmed
  self.status_confirmed = false if self.buyer_changed? || self.seller_changed?
end

Here, you are just assigning a value before saving it to db, so...

Hope it helps..

Upvotes: 9

Related Questions