# Dynamic validations

**URL:** <https://www.ruby-forum.com/t/dynamic-validations/161865>\
**Category:** Rails\
**Created:** [March 3, 2009, 6:46am UTC](https://www.ruby-forum.com/t/dynamic-validations/161865 "2009-03-03T06:46:33Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![timdunn2](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/54ee81/32.png) [@timdunn2](https://www.ruby-forum.com/u/timdunn2)\
**Post date:** [March 3, 2009, 6:46am UTC](https://www.ruby-forum.com/t/dynamic-validations/161865/1 "2009-03-03T06:46:33Z")

</div>

I am wondering what code I should put inside the class of my model to do  
validations for all columns of a given data type. For example I want to  
validate whether it is a number or not if it is of type integer. I want  
to do this so I don’t have to do individual validations on every column

---

<div class="post-metadata">

**Author:** ![timdunn2](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/54ee81/32.png) [@timdunn2](https://www.ruby-forum.com/u/timdunn2)\
**Post date:** [March 3, 2009, 7:04am UTC](https://www.ruby-forum.com/t/dynamic-validations/161865/2 "2009-03-03T07:04:57Z")

</div>

Tim Dunn wrote:

> I am wondering what code I should put inside the class of my model to do  
> validations for all columns of a given data type. For example I want to  
> validate whether it is a number or not if it is of type integer. I want  
> to do this so I don’t have to do individual validations on every column

Use validates\_numericality\_of :columnname

Tell your problem clearly so that we can try for solution…

---

<div class="post-metadata">

**Author:** ![timdunn2](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/54ee81/32.png) [@timdunn2](https://www.ruby-forum.com/u/timdunn2)\
**Post date:** [March 5, 2009, 3:41am UTC](https://www.ruby-forum.com/t/dynamic-validations/161865/3 "2009-03-05T03:41:26Z")

</div>

say i have five columns of type integer like: age, book\_number,  
page\_number, number\_of\_pages, number\_of\_people  
and instead of doing this  
validate\_numericality\_of :age, :book\_number, :page\_number,  
:number\_of\_pages, :number\_of\_people

I want to name all columns that are of type integer to an array and for  
every element of that array validate if its a number or not
