Rails 7.1 validate with endless and beginless range
#code #summary #rails71 #validation #validators
This is a summary of the article, and some of the code lines are taken from there:
https://www.bigbinary.com/blog/rails-7-adds-endless-ranges-for-activemodel-validations
Code:
# endless range example
class Person
validates_length_of :first_name, in: 20..
end
# endless range example
class User
validates_length_of :first_name, within: 20..
end
# endless and beginless range example
class Food
validates :created_at, inclusion: { in: proc { (..Date.today) } }
validates :expired_at, inclusion: { in: proc { (Date.today..) } }
end