# Using same validation-set in more than one class

**URL:** <https://www.ruby-forum.com/t/using-same-validation-set-in-more-than-one-class/126796>\
**Category:** Rails\
**Created:** [January 22, 2008, 10:16am UTC](https://www.ruby-forum.com/t/using-same-validation-set-in-more-than-one-class/126796 "2008-01-22T10:16:49Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![gabru](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/c0e974/32.png) [@gabru](https://www.ruby-forum.com/u/gabru)\
**Post date:** [January 22, 2008, 10:16am UTC](https://www.ruby-forum.com/t/using-same-validation-set-in-more-than-one-class/126796/1 "2008-01-22T10:16:49Z")

</div>

hello,  
lets describe my situation: i have two classes which have a lot same  
validations defined … to stay DRY i want to put them in one place and  
use it within both ActiveRecord classes.

- i think subclassing is not possible because both classes are AR of  
different tables

- tried to include a module but this didnt work either:

module CustomExtensions  
validates\_length\_of :a, :in =\> 1…10  
validates\_length\_of :b, :in =\> 1…20  
end

class A \< AR::Base  
include CustomExtensions  
end

thx for help!

---

<div class="post-metadata">

**Author:** ![gabru](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/c0e974/32.png) [@gabru](https://www.ruby-forum.com/u/gabru)\
**Post date:** [January 22, 2008, 10:35am UTC](https://www.ruby-forum.com/t/using-same-validation-set-in-more-than-one-class/126796/2 "2008-01-22T10:35:31Z")

</div>

On 22 Jan 2008, at 09:16, Michal G. wrote:

> - tried to include a module but this didnt work either:
> 
> module CustomExtensions  
> validates\_length\_of :a, :in =\> 1…10  
> validates\_length\_of :b, :in =\> 1…20  
> end
> 
> class A \< AR::Base  
> include CustomExtensions  
> end

nearly there!

module CommonStuff  
def self.included(base)  
base.instance\_eval do  
validates\_length\_of :a, :in =\> 1…10  
validates\_length\_of :b, :in =\> 1…20  
end  
end  
…  
end

class A \< AR::Base  
include CommonStuff  
end

---

<div class="post-metadata">

**Author:** ![gabru](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/c0e974/32.png) [@gabru](https://www.ruby-forum.com/u/gabru)\
**Post date:** [January 22, 2008, 10:44am UTC](https://www.ruby-forum.com/t/using-same-validation-set-in-more-than-one-class/126796/3 "2008-01-22T10:44:19Z")

</div>

hey thx for the quick reply … it worked!  
i am just wondering why its “instance\_eval” and not “class\_eval”…  
should the validations not be applied to the whole class?

> nearly there!
> 
> module CommonStuff  
> def self.included(base)  
> base.instance\_eval do  
> validates\_length\_of :a, :in =\> 1…10  
> validates\_length\_of :b, :in =\> 1…20  
> end  
> end  
> …  
> end
> 
> class A \< AR::Base  
> include CommonStuff  
> end

---

<div class="post-metadata">

**Author:** ![gabru](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/c0e974/32.png) [@gabru](https://www.ruby-forum.com/u/gabru)\
**Post date:** [January 22, 2008, 11:14am UTC](https://www.ruby-forum.com/t/using-same-validation-set-in-more-than-one-class/126796/4 "2008-01-22T11:14:15Z")

</div>

On 22 Jan 2008, at 09:44, Michal G. wrote:

> hey thx for the quick reply … it worked!  
> i am just wondering why its “instance\_eval” and not “class\_eval”…  
> should the validations not be applied to the whole class?

In this particular case it doesn’t make any difference. instance\_eval  
means that self will be the base class (ie A) when the validates\_\*  
calls are made. module\_eval (which is the same as class eval) would do  
the same thing (it would be different if we were defining methods  
inside the eval block, but we’re not)

Fred

---

<div class="post-metadata">

**Author:** ![gabru](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/c0e974/32.png) [@gabru](https://www.ruby-forum.com/u/gabru)\
**Post date:** [January 22, 2008, 11:44am UTC](https://www.ruby-forum.com/t/using-same-validation-set-in-more-than-one-class/126796/5 "2008-01-22T11:44:45Z")

</div>

On 22 Jan 2008, at 10:28, Michal G. wrote:

> i see… you mean if we would do this??

I mean  
module CommonStuff  
def self.included(base)  
base.instance\_eval do  
validates\_length\_of :a, :in =\> 1…10  
validates\_length\_of :b, :in =\> 1…20  
end  
end

has the same effect as

module CommonStuff  
def self.included(base)  
base.class\_eval do  
validates\_length\_of :a, :in =\> 1…10  
validates\_length\_of :b, :in =\> 1…20  
end  
end

but if your eval block had

def foo  
end  
then the method would be added in different places (A.foo instead of  
A.new.foo)

Fred

---

<div class="post-metadata">

**Author:** ![gabru](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/c0e974/32.png) [@gabru](https://www.ruby-forum.com/u/gabru)\
**Post date:** [January 22, 2008, 11:57am UTC](https://www.ruby-forum.com/t/using-same-validation-set-in-more-than-one-class/126796/6 "2008-01-22T11:57:51Z")

</div>

Frederick C. wrote:

oh i see now… thanks for your time! learned something again

> but if your eval block had
> 
> def foo  
> end  
> then the method would be added in different places (A.foo instead of  
> A.new.foo)
> 
> Fred

---

<div class="post-metadata">

**Author:** ![gabru](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/c0e974/32.png) [@gabru](https://www.ruby-forum.com/u/gabru)\
**Post date:** [January 22, 2008, 11:28am UTC](https://www.ruby-forum.com/t/using-same-validation-set-in-more-than-one-class/126796/7 "2008-01-22T11:28:18Z")

</div>

Frederick C. wrote:

> In this particular case it doesn’t make any difference. instance\_eval  
> means that self will be the base class (ie A) when the validates\_\*  
> calls are made. module\_eval (which is the same as class eval) would do  
> the same thing (it would be different if we were defining methods  
> inside the eval block, but we’re not)

i see… you mean if we would do this??

def self.included(base)  
class\_eval do  
…  
end  
end

> Fred
