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

**URL:** <https://discuss.rubyonrails.org/t/sharing-model-validation-code-in-lib-how-do-i-do-this/51497>\
**Category:** rubyonrails-talk\
**Created:** [May 8, 2010, 10:26pm UTC](https://discuss.rubyonrails.org/t/sharing-model-validation-code-in-lib-how-do-i-do-this/51497 "2010-05-08T22:26:05Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Matthew\_Hillsborough](https://avatars.discourse-cdn.com/v4/letter/m/d2c977/32.png) [@Matthew\_Hillsborough](https://discuss.rubyonrails.org/u/Matthew_Hillsborough)\
**Post date:** [May 8, 2010, 10:26pm UTC](https://discuss.rubyonrails.org/t/sharing-model-validation-code-in-lib-how-do-i-do-this/51497/1 "2010-05-08T22:26:05Z")

</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:** ![Anthony\_Gardner](https://avatars.discourse-cdn.com/v4/letter/a/bcef8e/32.png) [@Anthony\_Gardner](https://discuss.rubyonrails.org/u/Anthony_Gardner)\
**Post date:** [May 9, 2010, 10:35am UTC](https://discuss.rubyonrails.org/t/sharing-model-validation-code-in-lib-how-do-i-do-this/51497/2 "2010-05-09T10:35:18Z")

</div>

Maybe the following will help you …

### 6 Creating Custom Validation

Methods .. [http://guides.rails.info/activerecord\_validations\_callbacks.html](http://guides.rails.info/activerecord_validations_callbacks.html) and/or …

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