如何在Rails的模型中的字段中防止出现一系列术语?

问题描述

假设我们不想允许用户名banned = ['badWordOne','otherbadword','notthiseither']

中的任何一个

我们该怎么做?

我尝试使用acceptance,并直观地将accept更改为reject,但这是行不通的:

banned_names = ['badWordOne','notthiseither']

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

解决方法

我想我在exclusion

中找到了答案
banned_names = ['badWordOne','otherbadword','notthiseither']

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