# Sharing model validation code in lib/, how do I do this?

**URL:** <https://www.ruby-forum.com/t/sharing-model-validation-code-in-lib-how-do-i-do-this/188277>\
**Category:** Rails\
**Created:** [May 9, 2010, 12:27am UTC](https://www.ruby-forum.com/t/sharing-model-validation-code-in-lib-how-do-i-do-this/188277 "2010-05-09T00:27:36Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Matthew\_Hillsboroug](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ac91a4/32.png) [@Matthew\_Hillsboroug](https://www.ruby-forum.com/u/Matthew_Hillsboroug)\
**Post date:** [May 9, 2010, 12:27am UTC](https://www.ruby-forum.com/t/sharing-model-validation-code-in-lib-how-do-i-do-this/188277/1 "2010-05-09T00:27:36Z")

</div>

Hello everyone,

I’m having a bit of difficulty finding out the proper way to mix in code  
that I put into the lib/ directory for Rails 2.3.5.

I have several models that require phone validation. I had at least  
three  
models that used the same code, so I wanted to keep things DRY and moved  
it  
out to the lib/ directory. I used to have code like this in each model:

```
validate :phone_is_valid

```

Then I’d have a phone\_is\_valid method in the model:

```
protected
def phone_is_valid
  # process a bunch of logic
  errors.add_to_base("invalid phone") if validation failed
end

```

I moved this code out into lib/phones/ and in lib/phones I have  
lib/phones/phone\_validation.rb, and in there I copy pasted the  
phone\_is\_valid method.

My question is, how do I mix this into all of my models now? And does my  
validate :phone\_is\_valid method remain the same or does that change? I  
want  
to make sure that the errors.add\_to\_base method continues to function as  
it  
did before while keeping everything DRY. obviously the following won’t  
work:

```
validate :Phones::PhoneValidation::phone_is_valid(number)

```

I also created another file in lib/phones/ called  
lib/phones/phone\_normalize.rb. Again, many models need the value input  
by  
the user to be normalized. Meaning turn (555) 222-1212 to 5552221212 or  
something similar. Can I invoke that simply by invoking  
Phones::Phone\_Normalize::normalize\_method(number)?

I suppose I’m confused on the following:

- How to use the lib directory for validation of multiple models that  
need  
access to a particular validation method
- How to use the lib directory for commonly shared methods that return  
values

Thanks for the help,

Matthew.

---

<div class="post-metadata">

**Author:** ![Matthew\_Hillsboroug](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ac91a4/32.png) [@Matthew\_Hillsboroug](https://www.ruby-forum.com/u/Matthew_Hillsboroug)\
**Post date:** [May 9, 2010, 12:36pm UTC](https://www.ruby-forum.com/t/sharing-model-validation-code-in-lib-how-do-i-do-this/188277/2 "2010-05-09T12:36:51Z")

</div>

On 9 May 2010 00:26, Matthew Hillsborough  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)wrote:

> phone\_is\_valid method.  
> the user to be normalized. Meaning turn (555) 222-1212 to 5552221212 or  
> Thanks for the help,  
> For more options, visit this group at  
> [http://groups.google.com/group/rubyonrails-talk?hl=en](http://groups.google.com/group/rubyonrails-talk?hl=en).

Maybe the following will help you …

6 Creating Custom Validation Methods …

> **[rails.info](http://guides.rails.info/activerecord_validations_callbacks.html)**
>
> Ruby on Rails,Rails Tutorial,Ruby Basics,Rails 4,Bar Rails,Rail

and/or …

[http://marklunds.com/articles/one/312](http://marklunds.com/articles/one/312)
