开发者

trying to find duplicate usernames in ruby

开发者 https://www.devze.com 2022-12-14 09:36 出处:网络
i am trying to ensure that the username is unique when registering but im not sure how to do this I\'ve tried:

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?


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.


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消