开发者

Rails model validation - validates_inclusion_of

I have a string column in a table that can have a range of predefined values. It can also include a nil value. For ex: Dog, Cat, Bird, nil.

I want to write a validates_inclusion_of that checks to make sure all the values being entered fall within that predefined range. If, for ex, "Nasal Spray" is entered, it will throw an error.

What's the 开发者_如何学运维best way to do so?


Use the following validation within your model class:

validates_inclusion_of :animal, :in => %w(Dog Cat Bird), :allow_blank => true

—where :animal is the name of the column you want to validate.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜