# Updating attribute in model

**URL:** <https://www.ruby-forum.com/t/updating-attribute-in-model/150494>\
**Category:** Rails\
**Created:** [October 9, 2008, 4:09am UTC](https://www.ruby-forum.com/t/updating-attribute-in-model/150494 "2008-10-09T04:09:51Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![hangingclowns](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b5e925/32.png) [@hangingclowns](https://www.ruby-forum.com/u/hangingclowns)\
**Post date:** [October 9, 2008, 4:09am UTC](https://www.ruby-forum.com/t/updating-attribute-in-model/150494/1 "2008-10-09T04:09:51Z")

</div>

I’m trying to make a change to a field in the model whenever someone  
accesses it and it’s incorrect. So I have the following in my  
ActiveRecord class:

def status  
prev\_status = read\_attribute(:status)  
if end\_date \< Date.today  
update\_attribute(:status, “Expired”)  
end  
status  
end

I’m getting a “stack level too deep error”. Any suggestions on how to  
fix and the correct approach to this.

thanks

---

<div class="post-metadata">

**Author:** ![hangingclowns](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b5e925/32.png) [@hangingclowns](https://www.ruby-forum.com/u/hangingclowns)\
**Post date:** [October 9, 2008, 4:14am UTC](https://www.ruby-forum.com/t/updating-attribute-in-model/150494/2 "2008-10-09T04:14:49Z")

</div>

Hi Allen,

I’m not sure, but this seems to be a before or after save filter, if  
it’s before\_save, instead of update\_attribute, just call  
write\_attribute, when the model is saved it will automatically save  
this new value, if it’s an after filter, make it a before one.

On Wed, Oct 8, 2008 at 11:09 PM, Allen W.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> status

–  
Maurício Linhares  
[http://alinhavado.wordpress.com/](http://alinhavado.wordpress.com/) (pt-br) | [http://blog.codevader.com/](http://blog.codevader.com/)  
(en)  
João Pessoa, PB, +55 83 8867-7208

---

<div class="post-metadata">

**Author:** ![hangingclowns](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b5e925/32.png) [@hangingclowns](https://www.ruby-forum.com/u/hangingclowns)\
**Post date:** [October 9, 2008, 4:16am UTC](https://www.ruby-forum.com/t/updating-attribute-in-model/150494/3 "2008-10-09T04:16:00Z")

</div>

> end
> 
> I’m getting a “stack level too deep error”. Any suggestions on how to  
> fix and the correct approach to this.

You’re calling status from within status so it’s recursing forever.

Replace that last status with read\_attribute(:status) after perhaps  
reloading the object to ensure you’ve got the latest.

---

<div class="post-metadata">

**Author:** ![hangingclowns](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b5e925/32.png) [@hangingclowns](https://www.ruby-forum.com/u/hangingclowns)\
**Post date:** [October 9, 2008, 4:27am UTC](https://www.ruby-forum.com/t/updating-attribute-in-model/150494/4 "2008-10-09T04:27:25Z")

</div>

Philip H. wrote:

> > end
> > 
> > I’m getting a “stack level too deep error”. Any suggestions on how to  
> > fix and the correct approach to this.
> 
> You’re calling status from within status so it’s recursing forever.
> 
> Replace that last status with read\_attribute(:status) after perhaps  
> reloading the object to ensure you’ve got the latest.

Yeah that is was. Simple calling

‘read\_attribute(:status)’ instead of ‘status’ did the trick. thanks
