Forum: Ruby on Rails Validating Existing Models

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.
The B. (Guest)
on 2006-04-14 19:55
I've got a user object which has quite a few attributes mapped to the
database.  As of right now I've got some validations in the model like:

validates_presence_of :password, :message => "You must specify a valid
password."
validates_confirmation_of :password, :message => "Password doesn't match
the confirmation."
validates_presence_of :username, :message => "Username is required."
validates_format_of :email_address, :with =>
/^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i, :on => :create, :message =>
"Email address not a valid format."
validates_uniqueness_of :email_address, :scope => :email_address,
:message => "Email address has already been registered or is not valid."
validates_uniqueness_of :username, :scope => :username, :message =>
"Username is already taken."

It may be a bit of overkill, but anyways.  The problem I'm having is
that this works fine for registering a new user.  But when I want to
update the user I can't simply do:

user = User.update(params[:user]['id'].to_i, params[:user])

When I try this, the validation doesn't occur.  I want it to validate,
but ONLY certain things.  For example, I dont want it to validate the
username uniqueness on an update, or the email uniqueness if the user
didn't try to change it (but DO want to if they did change it).

Is there a way to specify different rules using the validate_on_update
overriding method or something similar?  The docs on validate_on_update
seem kind of slim so I'm not exactly sure how to implement this
conditional validation.
The B. (Guest)
on 2006-04-14 20:31
I hate replying to my own threads, but I did just realize that I can use
the validate_on_create and validate_on_update to manually create my own
validations instead of relying on the helpers.  This is great because it
gives me much finer control over my validation.

So, that being said, my next question relating to this is, can I access
the params from validate_on_update or create?

Right now, I found an article about creating custom accessors so I did
that and created new_password and password_confirmation that don't map
back to the database.  I set these values before called user.save.  It
works, but is there an easier way to create an accessor than this?

    def new_password=(pass)
        write_attribute("new_password", pass)
    end

    def new_password
        read_attribute("new_password")
    end

Any ideas?

The B. wrote:
> I've got a user object which has quite a few attributes mapped to the
> database.  As of right now I've got some validations in the model like:
>
> validates_presence_of :password, :message => "You must specify a valid
> password."
> validates_confirmation_of :password, :message => "Password doesn't match
> the confirmation."
> validates_presence_of :username, :message => "Username is required."
> validates_format_of :email_address, :with =>
> /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i, :on => :create, :message =>
> "Email address not a valid format."
> validates_uniqueness_of :email_address, :scope => :email_address,
> :message => "Email address has already been registered or is not valid."
> validates_uniqueness_of :username, :scope => :username, :message =>
> "Username is already taken."
>
> It may be a bit of overkill, but anyways.  The problem I'm having is
> that this works fine for registering a new user.  But when I want to
> update the user I can't simply do:
>
> user = User.update(params[:user]['id'].to_i, params[:user])
>
> When I try this, the validation doesn't occur.  I want it to validate,
> but ONLY certain things.  For example, I dont want it to validate the
> username uniqueness on an update, or the email uniqueness if the user
> didn't try to change it (but DO want to if they did change it).
>
> Is there a way to specify different rules using the validate_on_update
> overriding method or something similar?  The docs on validate_on_update
> seem kind of slim so I'm not exactly sure how to implement this
> conditional validation.
This topic is locked and can not be replied to.