On 11 August 2011 22:58, Leonel *.* <[email protected]> wrote: > I have a form where I can only update the user's first name, last name > and email address. > > I have this in the user model file, user.rb > > validates :username, > :presence => true, > :uniqueness => true, > :on => :update > > validates :password, > :presence => true, > :length => { :minimum => 6, :maximum => 20}, > :confirmation => true, > :on => :update > > But everytime I try to change the user's first name, I get the error > messages that the username is already taken and such. I'm not even > trying to update the username. And there is NOT EVEN A FIELD for the > username in the form.
Just to add to other posts, validations are nothing to do with data in fields on the form, or even actions in controllers, they are run by the model when a database record is created or updated by any method, the validation has no knowledge of where the data in the record being written came from. Having said that I do not understand your comment above, you say you get the problem when you try to change the username, but then say that there is not a field for username in the form. However validates_uniqueness_of should not be failing in the way you describe on update, and if you want to ensure uniqueness then you should be running the validation on update (and create). Perhaps some controller code (just the action that is causing the problem) would be interesting to see. Have you got any callback filters on the model that might be confusing the issue? On a separate note, if you wish a field to be unique then validates_uniqueness_of is not sufficient under some conditions (google will explain why I am sure) so you should also put a constraint in the database. Colin -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

