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:

# Code Summary for article # https://www.bigbinary.com/blog/rails-7-adds-endless-ranges-for-activemodel-validations # Works on Rails 7.1 # 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

Code:

# endless range exampleclass Person validates_length_of :first_name, in: 20..end# endless range exampleclass User validates_length_of :first_name, within: 20..end# endless and beginless range exampleclass Food validates :created_at, inclusion: { in: proc { (..Date.today) } } validates :expired_at, inclusion: { in: proc { (Date.today..) } }end

Reply

or to participate.