# DRY model validation question

**URL:** <https://www.ruby-forum.com/t/dry-model-validation-question/176766>\
**Category:** Rails\
**Created:** [October 6, 2009, 1:43am UTC](https://www.ruby-forum.com/t/dry-model-validation-question/176766 "2009-10-06T01:43:51Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dudebot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e495f1/32.png) [@Dudebot](https://www.ruby-forum.com/u/Dudebot)\
**Post date:** [October 6, 2009, 1:43am UTC](https://www.ruby-forum.com/t/dry-model-validation-question/176766/1 "2009-10-06T01:43:51Z")

</div>

Here’s a question:

I have the exact same validation code in 2 different models.

OK, you’re thinking, combine the models. Not in this case.

So, in Rails, is there a way to reference the same validation for 2  
different models?

E.g.

validate :foo

def foo  
bar  
end

Appears in /models/a.rb and /models/b.rb

Can I make foo DRY?

TIA,  
Craig

---

<div class="post-metadata">

**Author:** ![Dudebot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e495f1/32.png) [@Dudebot](https://www.ruby-forum.com/u/Dudebot)\
**Post date:** [October 6, 2009, 3:29am UTC](https://www.ruby-forum.com/t/dry-model-validation-question/176766/2 "2009-10-06T03:29:48Z")

</div>

On Oct 5, 4:42 pm, Dudebot [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Here’s a question:
> 
> I have the exact same validation code in 2 different models.
> 
> OK, you’re thinking, combine the models. Not in this case.
> 
> So, in Rails, is there a way to reference the same validation for 2  
> different models?

Commonly you can find this done with validation plugins.

See for example,

[Peter Marklund's Home](http://marklunds.com/articles/one/312)

for an email address validation, which is then available in all  
ActiveRecord models through

class A \< ActiveRecord::Base

validates\_as\_email :email\_address

end

Stephan

---

<div class="post-metadata">

**Author:** ![Dudebot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e495f1/32.png) [@Dudebot](https://www.ruby-forum.com/u/Dudebot)\
**Post date:** [October 6, 2009, 2:02pm UTC](https://www.ruby-forum.com/t/dry-model-validation-question/176766/3 "2009-10-06T14:02:39Z")

</div>

Dudebot wrote:

> Here’s a question:
> 
> I have the exact same validation code in 2 different models.
> 
> OK, you’re thinking, combine the models.

That shouldn’t ever be your first reaction. First reaction is to  
refactor out the commonalities.

If either, or both models end up as skeletons, then you should rethink  
your original design… why were they separate in the first place?  
Lack of analysis. or alot of analysis that you’ve forgotten that tiny  
important bit form which led you to separate them in the first place.

> So, in Rails, is there a way to reference the same validation for 2  
> different models?

As mentioned previously, plugins, or a parent abstract class depending  
on the breadth of commonalities.

---

<div class="post-metadata">

**Author:** ![Dudebot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e495f1/32.png) [@Dudebot](https://www.ruby-forum.com/u/Dudebot)\
**Post date:** [October 7, 2009, 6:24pm UTC](https://www.ruby-forum.com/t/dry-model-validation-question/176766/4 "2009-10-07T18:24:02Z")

</div>

Thanks, Stephan, Ar & Lake! Ar: sorry about the sloppy use of the  
word “combine” – I meant refactor 🙂

On Oct 6, 12:36 pm, Lake D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![Dudebot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e495f1/32.png) [@Dudebot](https://www.ruby-forum.com/u/Dudebot)\
**Post date:** [October 6, 2009, 7:36pm UTC](https://www.ruby-forum.com/t/dry-model-validation-question/176766/5 "2009-10-06T19:36:06Z")

</div>

Hi Craig,

My solution to this problem can be found here:

> <https://gist.github.com/ldenman/203225>

Basically, I just add the validations in a module as instance methods  
(def foo…), include that module in the models, and as the module is  
included, tell the “klass” to validate the instance methods.

Make sense?
