Validation problems

Hi there!

I have a problem with validations: My user model allways validates the
presence of login, password and password_confirmation -
it also does this if I use update_attributes to update a single
attribute.

So for example updating “username” fails, because I of course do NOT
provide
password, password_confirmation etc. when only updating one attribute.

So: How can I make stuff like “username” updateable by update_attributes
and
still make sure that login, password etc have to be validaten when I
do not change a single attribute but the entire record?

Marcel Buchholz wrote:

Hi there!

I have a problem with validations: My user model allways validates the
presence of login, password and password_confirmation -
it also does this if I use update_attributes to update a single
attribute.

So for example updating “username” fails, because I of course do NOT
provide
password, password_confirmation etc. when only updating one attribute.

So: How can I make stuff like “username” updateable by update_attributes
and
still make sure that login, password etc have to be validaten when I
do not change a single attribute but the entire record?

Something like this should work:

validates_confirmation_of :password, :if => :need_to_confirm_pw

private

def need_to_confirm_pw
return true if new_record?
return self.class.find(id).password != self[‘password’]
end

This assumes that whatever processing you do to the password field has
already been done on assignment.

–Al Evans