# Problem extending ActiveRecord with custom validations

**URL:** <https://www.ruby-forum.com/t/problem-extending-activerecord-with-custom-validations/155243>\
**Category:** Rails\
**Created:** [December 9, 2008, 8:05pm UTC](https://www.ruby-forum.com/t/problem-extending-activerecord-with-custom-validations/155243 "2008-12-09T20:05:40Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![gaveeno](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/58956e/32.png) [@gaveeno](https://www.ruby-forum.com/u/gaveeno)\
**Post date:** [December 9, 2008, 8:05pm UTC](https://www.ruby-forum.com/t/problem-extending-activerecord-with-custom-validations/155243/1 "2008-12-09T20:05:40Z")

</div>

I’m having a problem extending ActiveRecord with custom validations  
based on the “Write Custom Validations” recipe from Advanced Rails  
Recipes. As far as I can tell, I’ve followed the instructions exactly  
but for some reason this isn’t working and it’s driving me crazy.  
I’ve tried making the methods in the custom validations module class  
methods (self.validates…) as well as the way they are below (which  
is the way they are in the instructions).

Any suggestions?

Thanks!  
Gavin

* * *

lib/custom\_validations.rb

module CustomValidations  
def validates\_phone(\*attr\_names)  
attr\_names.each do |attr\_name|  
validates\_format\_of attr\_name,  
:with =\> /^[()0-9- +.]{10,20} \*[extension.]{0,9} \*  
[0-9]{0,5}$/i,  
:message =\> “invalid”  
end  
end  
def validates\_email(\*attr\_names)  
attr\_names.each do |attr\_name|  
validates\_format\_of attr\_name,  
:with =\> /(^([^@\s]+)@((?:[-\_a-z0-9]+.)+[a-z]{2,})$)|(^$)/  
i,  
:message =\> “invalid”  
end  
end  
end  
ActiveRecord::Base.extend(CustomValidations)

* * *

config/environment.rb

# Be sure to restart your server when you modify this file

# Uncomment below to force Rails into production mode when

# you don’t control web/app server and can’t set it the proper way

ENV[‘RAILS\_ENV’] ||= ‘production’

#normal environment.rb stuff

end

require ‘custom\_validations’

* * *

models/user.rb

class User \< ActiveRecord::Base  
validates\_email :email  
end

* * *

error from console when starting server

/vendor/rails/activerecord/lib/active\_record/base.rb:1667:in  
`method_missing': undefined method`validates\_email’ for #\<Class:  
0x31ac9c8\> (NoMethodError)

---

<div class="post-metadata">

**Author:** ![gaveeno](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/58956e/32.png) [@gaveeno](https://www.ruby-forum.com/u/gaveeno)\
**Post date:** [December 9, 2008, 8:10pm UTC](https://www.ruby-forum.com/t/problem-extending-activerecord-with-custom-validations/155243/2 "2008-12-09T20:10:23Z")

</div>

On 9 Dec 2008, at 19:05, gaveeno wrote:

> 

Have you tried requiring it from an initializer, not the bottom of  
environment.rb ?

Fred

---

<div class="post-metadata">

**Author:** ![gaveeno](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/58956e/32.png) [@gaveeno](https://www.ruby-forum.com/u/gaveeno)\
**Post date:** [December 9, 2008, 8:18pm UTC](https://www.ruby-forum.com/t/problem-extending-activerecord-with-custom-validations/155243/3 "2008-12-09T20:18:00Z")

</div>

Thanks Fred, that worked! I created config/initializers/  
custom\_validations.rb and moved the require line there from the  
environment.rb file. It’s not clear to me why this works, and it  
seems like it’s overkill to create a new file for this, but as long as  
it works it’s cool with me.

Thanks again.  
-Gavin

On Dec 9, 11:09 am, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![gaveeno](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/58956e/32.png) [@gaveeno](https://www.ruby-forum.com/u/gaveeno)\
**Post date:** [December 9, 2008, 8:40pm UTC](https://www.ruby-forum.com/t/problem-extending-activerecord-with-custom-validations/155243/4 "2008-12-09T20:40:54Z")

</div>

On Dec 9, 7:17 pm, gaveeno [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Thanks Fred, that worked! I created config/initializers/  
> custom\_validations.rb and moved the require line there from the  
> environment.rb file. It’s not clear to me why this works, and it

Short version: because it means the require will happen at the right  
time.  
Long version:

> **[environment.rb and requiring dependencies - Space Vatican](https://www.spacevatican.org/2008/11/21/environment-rb-and-requiring-dependencies/)**
>
> In the before time, the bottom of most of my apps’ environment.rb was an unholy mess. Inflector rules, requiring of various libraries or gems, …

Fred

---

<div class="post-metadata">

**Author:** ![gaveeno](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/58956e/32.png) [@gaveeno](https://www.ruby-forum.com/u/gaveeno)\
**Post date:** [December 9, 2008, 8:49pm UTC](https://www.ruby-forum.com/t/problem-extending-activerecord-with-custom-validations/155243/5 "2008-12-09T20:49:46Z")

</div>

Thanks Fred, interesting blog post!

On Dec 9, 11:40 am, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
