views:

102

answers:

3

hi i am trying to ensure that the username is unique when registering but im not sure how to do this

I've tried:

validates_uniqueness_of :username

but it doesnt work

it gives me this error: undefined method 'validates_uniqueness_of' for #<UsersController:0x6c4fd2>

Any help please?

+1  A: 

validates_uniqueness_of should work, however you have to add unique index to the database column as well to avoid race conditions. This can be done via migration:

add_index :table_name, :column_name , :unique=> true

Check the case_sensitive option for validates_uniqueness_of as well.

khelll
+6  A: 

You haven't specified where you have declared validates_uniqueness_of. It's a class method mixed into ActiveRecord::Base, so make sure you're declaring it inside of a model, not in a controller or somewhere else.

Steve Graham
my bad. thx very much !
Lily
+1  A: 

This needs to be placed into the User model not the controller.

Ryan Bigg