Update_attribute with Validation?


#1

Does anyone know of a way to update individual attributes with
validation? update_attribute disables the validation checks in the
record model, so I’m wondering if there’s a workaround for this.

In my application, I want to allow the user to update some or all the
attributes without having to enter :password and :password_confirmation,
both of which are subject to validation.

Boram


#2

On 2/27/06, Boram Y. removed_email_address@domain.invalid wrote:

Does anyone know of a way to update individual attributes with
validation? update_attribute disables the validation checks in the
record model, so I’m wondering if there’s a workaround for this.

In my application, I want to allow the user to update some or all the
attributes without having to enter :password and :password_confirmation,
both of which are subject to validation.

Boram

You can disable validations with the :if option.

class User < AR::Base
attr_accessor :skip_password
validates_confirmation_of :password, :if => :password_required?

def password_required?
@skip_password != true
end
end

@user.skip_password = true
@user.update_attributes(…)


Rick O.
http://techno-weenie.net


#3

Boram Y. wrote:

Does anyone know of a way to update individual attributes with
validation? update_attribute disables the validation checks in the
record model, so I’m wondering if there’s a workaround for this.

In my application, I want to allow the user to update some or all the
attributes without having to enter :password and :password_confirmation,
both of which are subject to validation.

update_attributes (note the plural) call validation before updating, so
why not use that?


Lee


#4

I should’ve tested out my theory before posting that previous post.

I added the following to my user model, which fixed my problem:

def before_update
self.hashed_password = User.hash_password(self.password)
end

Thanks again for your help, Rick.


#5

Because there are times when the user will not want to update their
password, and password and password_confirmation both have
validates_presence_of and validates_confirmation_of applied to them.

Rick, thanks very, very much for responding. The solution you gave works
for the case when I want to bypass password and password confirmation,
but does not for the case when I want to update the password. I think
the problem probably has to do with the fact that I’m using a hashed
password in my user model. I’ll post here if and when I find a work
around (of course, if anyone has already been through this issue, feel
free to enlighten my feeble mind).

Thanks!

Lee O’Mara wrote:

Boram Y. wrote:

Does anyone know of a way to update individual attributes with
validation? update_attribute disables the validation checks in the
record model, so I’m wondering if there’s a workaround for this.

In my application, I want to allow the user to update some or all the
attributes without having to enter :password and :password_confirmation,
both of which are subject to validation.

update_attributes (note the plural) call validation before updating, so
why not use that?


Lee