Help: Validating when Updating a Record

Hi

For my users model, the password field cannot be the same as the
username, which is checked by the validate method in the model:

class User < ActiveRecord::Base
  ...
  def validate
    if password == username
      errors.add(:password, "Cannot be the username")
    end
  end
end

This works fine when creating a new user. However it fails when a
user wants to change their password.

The reason it fails is that the update/edit form view does not let
the user change their username (usernames are static). So when the
validate method (above) is ran it does not know what the username is.

What is the rails-way for tackling this?

thanks!

Well, I’ve partly fixed it by including a username hidden field.

That way the username pararmeter will always be passed

Not sure if thats the rails-way or not? :slight_smile:

On 10/9/06, Jason B [email protected] wrote:

     errors.add(:password, "Cannot be the username")

validate method (above) is ran it does not know what the username is.

What is the rails-way for tackling this?

thanks!

I would think that if your table has a username field (called username)
in
it then this would not be a problem.

Can you post some more information on your model? Relationships /
validtaions. Not sure if it will help but some more information would
be
good.

Cheers

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs