简体   繁体   中英

rails validates with inclusion and one value

I have this in my Foo class :

MIN = 2
MAX = 42

validates :max_users,
            presence: true,
            inclusion: { in: MIN..MAX }

I need to allow "0" value AND the MIN..MAX interval, how can I add zero as possible value?

You can pass an array of values for in option as well:

validates :max_users,
            presence: true,
            inclusion: { in: (MIN..MAX).to_a + [0] }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM