On model update attribute

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 ?

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

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

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. [email protected]

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?