in Programming, Uncategorized

SystemStackError: stack level too deep

I was going to update a record through the form, but when I submitted the form, I was presented with the following error

SystemStackError in ProductsController#update
stack level too deep

After looking around I found this question on StackOverflow with a helpful answer. stack level too deep error

This error generally happens when you accidentally recursively changing an attribute. If you have a username attribute in User model, and a virtual attribute named username, that is directly changing the username, you end up calling the virtual, the virtual calls the virtual again and so on.. Therefore, take a look on whether something like that happens somewhere in your code. – Spyros

I looked within my Product model, and sure enough I was calling a virtual attribute recursively. Hate typos.

def price_in_dollars=(dollars)                                           
  self.price_in_dollars = dollars.to_d * 100 if dollars.present?         
end

I changed price_in_dollars to price_in_cents and everything was fixed.

def price_in_dollars=(dollars)                                           
  self.price_in_cents = dollars.to_d * 100 if dollars.present?         
end

Write a Comment

Comment