# Rails model validation: Can I create my own validations?

**URL:** <https://www.ruby-forum.com/t/rails-model-validation-can-i-create-my-own-validations/104845>\
**Category:** Rails\
**Created:** [May 23, 2007, 5:07am UTC](https://www.ruby-forum.com/t/rails-model-validation-can-i-create-my-own-validations/104845 "2007-05-23T05:07:00Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![greg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/71c47a/32.png) [@greg](https://www.ruby-forum.com/u/greg)\
**Post date:** [May 23, 2007, 5:07am UTC](https://www.ruby-forum.com/t/rails-model-validation-can-i-create-my-own-validations/104845/1 "2007-05-23T05:07:00Z")

</div>

Hi,

Is it possible to create my own validations within the Rails model  
validation framework?

In particular I’m interesting in being able to create a global (i.e. not  
associated with a particular field) business logic validation  
exception(s)  
to pass back to the controller. They could then be listed on the View  
as an  
exception error, but not associated with a particular field.

I’m thinking for higher level business logic rule exceptions like this  
it  
may be better doing it the above way as opposed to creating an exception  
(  
e.g. businessLogic exception class) - but any ideas/comments welcome on  
this.

Tks  
Greg

---

<div class="post-metadata">

**Author:** ![greg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/71c47a/32.png) [@greg](https://www.ruby-forum.com/u/greg)\
**Post date:** [May 23, 2007, 6:06am UTC](https://www.ruby-forum.com/t/rails-model-validation-can-i-create-my-own-validations/104845/2 "2007-05-23T06:06:16Z")

</div>

all rails model validators (ie. validates\_uniqueness\_of) are based on  
validates\_each

so all you have to do is

def self.validates\_something(\*args)  
validates\_each(\*args){|column|  
errors.add(blah blah blah) if not something  
}  
end

---

<div class="post-metadata">

**Author:** ![greg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/71c47a/32.png) [@greg](https://www.ruby-forum.com/u/greg)\
**Post date:** [May 23, 2007, 6:39pm UTC](https://www.ruby-forum.com/t/rails-model-validation-can-i-create-my-own-validations/104845/3 "2007-05-23T18:39:41Z")

</div>

You can add any validation logic you like to the validate method on  
any active model class. It can validate any combination of attributes  
of the object.

If you are wanting to add new meta-programming methods like  
validate\_presence\_of that takes some additional digging I have not  
done yet.

Michael

On May 22, 8:05 pm, “Greg H.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![greg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/71c47a/32.png) [@greg](https://www.ruby-forum.com/u/greg)\
**Post date:** [May 24, 2007, 5:20am UTC](https://www.ruby-forum.com/t/rails-model-validation-can-i-create-my-own-validations/104845/4 "2007-05-24T05:20:51Z")

</div>

On 5/24/07, MichaelLatta [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> If you are wanting to add new meta-programming methods like  
> validate\_presence\_of that takes some additional digging I have not  
> done yet.
> 
> Michael

Most of those just wrap #validates\_each, which is documented.

---

<div class="post-metadata">

**Author:** ![greg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/71c47a/32.png) [@greg](https://www.ruby-forum.com/u/greg)\
**Post date:** [May 24, 2007, 8:53am UTC](https://www.ruby-forum.com/t/rails-model-validation-can-i-create-my-own-validations/104845/5 "2007-05-24T08:53:05Z")

</div>

How does this look (I’m not at my rails PC at the moment)? i.e.  
regarding  
the use of Global (not attribute specific) validations within Model

* * *

## class Person \< ActiveRecord::Base protected def validate\_on\_create if !complex\_business\_logic\_passes? errors.add\_to\_base(“complex business logic failed”) end end def complex\_business\_logic\_passes? … return result # true if passes end end
 Business Logic Errors = \<%= error\_message\_on "object", "base" %\> 
* * *
