# On model update attribute

**URL:** <https://www.ruby-forum.com/t/on-model-update-attribute/129047>\
**Category:** Rails\
**Created:** [February 13, 2008, 8:13pm UTC](https://www.ruby-forum.com/t/on-model-update-attribute/129047 "2008-02-13T20:13:54Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![KTU](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/ecae2f/32.png) [@KTU](https://www.ruby-forum.com/u/KTU)\
**Post date:** [February 13, 2008, 8:13pm UTC](https://www.ruby-forum.com/t/on-model-update-attribute/129047/1 "2008-02-13T20:13:54Z")

</div>

Hi

any idea why on model this wont work:

class User \< ActiveRecord::Base

def approve  
User.find(params[:id]).update\_attribute(‘status’,“1”)  
end  
end

now I have controller with approve action and I see from the log that  
sql statement goes and updates  
that record but status wont change to 1.

any ideas ?

---

<div class="post-metadata">

**Author:** ![KTU](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/ecae2f/32.png) [@KTU](https://www.ruby-forum.com/u/KTU)\
**Post date:** [February 13, 2008, 8:26pm UTC](https://www.ruby-forum.com/t/on-model-update-attribute/129047/2 "2008-02-13T20:26:36Z")

</div>

how exactly do you see this?

> I see from the log that  
> sql statement goes and updates  
> that record but status wont change to 1

so you see the sql statement in the log and the change in the database?  
but any later request returns the wrong value?  
or you see the sql statement and nothing changes in the db

or does any ‘older’ user object not reflect the changes? that would be  
normal

---

<div class="post-metadata">

**Author:** ![KTU](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/ecae2f/32.png) [@KTU](https://www.ruby-forum.com/u/KTU)\
**Post date:** [February 13, 2008, 8:29pm UTC](https://www.ruby-forum.com/t/on-model-update-attribute/129047/3 "2008-02-13T20:29:51Z")

</div>

are you sure the column in which you’re putting the number 1 is a  
string? (that’s what you’re passing it)

---

<div class="post-metadata">

**Author:** ![KTU](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/ecae2f/32.png) [@KTU](https://www.ruby-forum.com/u/KTU)\
**Post date:** [February 13, 2008, 8:35pm UTC](https://www.ruby-forum.com/t/on-model-update-attribute/129047/4 "2008-02-13T20:35:43Z")

</div>

yes that was it. it was defined as tinyint i changed it to int. works  
now thanks man

On Feb 13, 9:29 pm, Phil T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![KTU](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/ecae2f/32.png) [@KTU](https://www.ruby-forum.com/u/KTU)\
**Post date:** [February 14, 2008, 8:29pm UTC](https://www.ruby-forum.com/t/on-model-update-attribute/129047/5 "2008-02-14T20:29:26Z")

</div>

This may be a bit subtle, but you might want to encapsulate the  
concept of “approve” into the model. As it stands presently, you’ve  
essentially mixed some business logic (what it means to approve a  
user) into your controller. If you ever decide to change that concept  
you’re going to have to visit the controller… and every other place  
that relies on User.status = 1 meaning “approved”. You might consider  
something like this:

class User  
def approve  
update\_attributes(:status, 1)  
end

def approved?  
self.status==1  
end  
…  
end

By doing this you’ve encapsulated the ‘approved’ concept into your  
model and you can change your mind about the implementation later. For  
example, maybe you want to use the acts\_as\_state\_machine plugin and  
introduce the idea of suspending the user. That would likely change  
how you approve the user and how you test for it. With the code above  
you can simply say…

@user = User.find(params[:id])  
@user.approve

redirect :back unless @user.approved?
