Help: Validating when Updating a Record


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")

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?


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:

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.