# Adding custom validations to active record

**URL:** <https://www.ruby-forum.com/t/adding-custom-validations-to-active-record/161011>\
**Category:** Rails\
**Created:** [February 22, 2009, 10:24am UTC](https://www.ruby-forum.com/t/adding-custom-validations-to-active-record/161011 "2009-02-22T10:24:26Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![sa125](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b4bc9f/32.png) [@sa125](https://www.ruby-forum.com/u/sa125)\
**Post date:** [February 22, 2009, 10:24am UTC](https://www.ruby-forum.com/t/adding-custom-validations-to-active-record/161011/1 "2009-02-22T10:24:26Z")

</div>

Hi - I want to add my own validation to a rails app, and need help  
making it global to my app. Basically, the validation makes sure a  
number is positive - I found this code on the at  
[http://snippets.dzone.com/posts/show/822:](http://snippets.dzone.com/posts/show/822:)

module CustomValidations

def validates\_positive\_or\_zero(\*attr\_names)  
configuration = { :message =\> “Cannot be negative” }  
configuration.update(attr\_names.pop) if attr\_names.last.is\_a?(Hash)  
validates\_each attr\_names do |m, a, v|  
m.errors.add(a, configuration[:message]) if v\<0  
end  
end

end

I want to use it in my models like any other validation:

class SomeModel \< ActiveRecord::Base  
validates\_positivity\_of :some\_number, :another\_number  
end

I saved it in lib/custom\_validations.rb, and tried including it in the  
top of my environment.rb to extend AR:

include ‘custom\_validations’  
class ActiveRecord::Base  
include CustomValidations  
end

#… rest of file

Sadly, this won’t work. How do I make rails identify this module and use  
it properly?

---

<div class="post-metadata">

**Author:** ![sa125](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b4bc9f/32.png) [@sa125](https://www.ruby-forum.com/u/sa125)\
**Post date:** [February 22, 2009, 10:31am UTC](https://www.ruby-forum.com/t/adding-custom-validations-to-active-record/161011/2 "2009-02-22T10:31:23Z")

</div>

sa 125 wrote:

> Sadly, this won’t work.  
> Always include even a snip of the error message you got. After your  
> detailed  
> writeup, just claiming “won’t work” leaves us bereft of closure!

However, the top of environment.rb might be before “require  
‘active\_record’”.  
That means ‘class’ would introduce ‘ActiveRecord’, leaving ‘Base’ as a  
missing  
identifier. If so, move it to the bottom!

–  
Phlip

---

<div class="post-metadata">

**Author:** ![sa125](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b4bc9f/32.png) [@sa125](https://www.ruby-forum.com/u/sa125)\
**Post date:** [February 22, 2009, 10:44am UTC](https://www.ruby-forum.com/t/adding-custom-validations-to-active-record/161011/3 "2009-02-22T10:44:59Z")

</div>

my bad. I moved that block to the bottom of the environment.rb file,  
after the “end” of the Rails::Initializer code, but got this error:

NoMethodError (undefined method ‘validates\_positivity\_of’ for  
#\<Class…\>)  
/…/active\_record/base.rb:1833:in ‘method missing’

I tried loading this method though ./script/console:  
irb\> include CustomValidations  
irb\> validates\_positivity\_of 2  
NoMethodError: undefined method ‘validates\_each’ for #\<object…\>  
from /…/lib/custom\_validations.rb:6:in ‘validates\_positivity\_of’

Now I’m not even sure if the function works properly.

---

<div class="post-metadata">

**Author:** ![sa125](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b4bc9f/32.png) [@sa125](https://www.ruby-forum.com/u/sa125)\
**Post date:** [February 22, 2009, 12:56pm UTC](https://www.ruby-forum.com/t/adding-custom-validations-to-active-record/161011/4 "2009-02-22T12:56:07Z")

</div>

It isn`t include in your case, it`s extend:

include ‘custom\_validations’  
class ActiveRecord::Base  
extend CustomValidations  
end

Here’s a better explanation why →   
[http://blog.codevader.com/2008/09/27/including-and-extending-modules-in-ruby/](http://blog.codevader.com/2008/09/27/including-and-extending-modules-in-ruby/)

- 

Maurício Linhares  
[http://alinhavado.wordpress.com/](http://alinhavado.wordpress.com/) (pt-br) | [http://blog.codevader.com/](http://blog.codevader.com/)  
(en)

On Sun, Feb 22, 2009 at 6:24 AM, sa 125
