How to prevent any in an array of terms in a field in a model in rails?

33 Views Asked by At

Suppose we don't want to allow username to be any of banned = ['badWordOne', 'otherbadword', 'notthiseither']

How do we do that?

I tried using acceptance and intuitively changed accept to reject, but that doesn't work:

banned_names = ['badWordOne', 'otherbadword', 'notthiseither']

validates :username, acceptance: { reject: banned_names }, if: :username 

1

There are 1 best solutions below

3
stevec On

I think I found the answer in exclusion

banned_names = ['badWordOne', 'otherbadword', 'notthiseither']

validates :username, exclusion: { in: banned_names }, if: :username