开发者

Rails model validation - validates_inclusion_of

开发者 https://www.devze.com 2023-01-05 14:47 出处:网络
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 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

精彩评论

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