Forum: Ruby on Rails update_attribute with Validation?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
C8134e7a4b73fc95b52114d7dba8971b?d=identicon&s=25 mofo mofo (boram)
on 2006-02-27 14:55
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
821395fe70906c8290df7f18ac4ac6cf?d=identicon&s=25 Rick Olson (Guest)
on 2006-02-27 15:52
(Received via mailing list)
On 2/27/06, Boram Yoon <boram.yoon@gmail.com> 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 Olson
http://techno-weenie.net
C9806138c2b665621a723f0c88aff221?d=identicon&s=25 Lee O'Mara (Guest)
on 2006-02-28 01:15
(Received via mailing list)
Boram Yoon 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
C8134e7a4b73fc95b52114d7dba8971b?d=identicon&s=25 mofo mofo (boram)
on 2006-02-28 10:13
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 Yoon 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
C8134e7a4b73fc95b52114d7dba8971b?d=identicon&s=25 mofo mofo (boram)
on 2006-02-28 10:25
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.
This topic is locked and can not be replied to.