# Newbie seeking advice on DRYing up my model validations

**URL:** <https://www.ruby-forum.com/t/newbie-seeking-advice-on-drying-up-my-model-validations/186166>\
**Category:** Rails\
**Created:** [April 1, 2010, 2:53pm UTC](https://www.ruby-forum.com/t/newbie-seeking-advice-on-drying-up-my-model-validations/186166 "2010-04-01T14:53:05Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![grar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/cc9497/32.png) [@grar](https://www.ruby-forum.com/u/grar)\
**Post date:** [April 1, 2010, 2:53pm UTC](https://www.ruby-forum.com/t/newbie-seeking-advice-on-drying-up-my-model-validations/186166/1 "2010-04-01T14:53:05Z")

</div>

Hi,

Despite close reading of Matz’s Ruby book and Agile Web Dev. w. Rails,  
I can’t figure out how to tackle the challenge of sharing validation  
code across models with attributes in common.

In each of my models I’d like to validate the shared attributes with a  
custom validator that includes Rails ActiveRecord::Validations class  
methods and some of my own validations. My problem is 1) I don’t know  
how to pass attribute references to my special method and 2) (mundane)  
I can’t successfully call any ActiveRecord::Validations class methods  
from within my custom method.

A bit of garbage here, I know, but any thoughts? Is this kind of  
problem handled by use of certain gems?

Thanks,

Grar

---

<div class="post-metadata">

**Author:** ![grar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/cc9497/32.png) [@grar](https://www.ruby-forum.com/u/grar)\
**Post date:** [April 1, 2010, 3:42pm UTC](https://www.ruby-forum.com/t/newbie-seeking-advice-on-drying-up-my-model-validations/186166/2 "2010-04-01T15:42:51Z")

</div>

Hi Grar,

If you have a User model where you want to use some shared validations,  
you  
could create a module like this â€“ in lib/my\_email\_validations.rb:

module MyEmailValidations  
def self.included(base)  
base.validates\_presence\_of :email  
base.validates\_length\_of :email, :minimum =\> 6  
base.validate :email\_must\_be\_from\_valid\_provider  
end

```
# example of a custom validator
def email_must_be_from_valid_provider
  errors.add :email, "must be from a valid provider" if
    email !~ /@gmail.com$/
end

```

end

and in your User model, include them like this:

class User \< ActiveRecord::Base  
include MyEmailValidations  
end

Does this solve your problem?

/Lasse

2010/4/1 Grary [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![grar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/cc9497/32.png) [@grar](https://www.ruby-forum.com/u/grar)\
**Post date:** [April 1, 2010, 5:06pm UTC](https://www.ruby-forum.com/t/newbie-seeking-advice-on-drying-up-my-model-validations/186166/3 "2010-04-01T17:06:34Z")

</div>

Lasse,

Yes, that was exactly what I needed.

I see I need to explore the specifics of mixing in (which maybe Matz’s  
book underserves) – in this case it was the self.include(base)  
expression that was key.

Finally, where attributes refer logically to the same kind of data,  
but have different names, I have aliased them to allow the broadest  
use of my version of your validation module.

Thanks so much,

Grar

---

<div class="post-metadata">

**Author:** ![grar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/cc9497/32.png) [@grar](https://www.ruby-forum.com/u/grar)\
**Post date:** [April 1, 2010, 5:11pm UTC](https://www.ruby-forum.com/t/newbie-seeking-advice-on-drying-up-my-model-validations/186166/4 "2010-04-01T17:11:14Z")

</div>

Nice â€“ glad you could use it 🙂

/Lasse

2010/4/1 Grary [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
