# Storing Active record validation code in the database

**URL:** <https://www.ruby-forum.com/t/storing-active-record-validation-code-in-the-database/165756>\
**Category:** Rails\
**Created:** [April 17, 2009, 3:01am UTC](https://www.ruby-forum.com/t/storing-active-record-validation-code-in-the-database/165756 "2009-04-17T03:01:40Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![interactivenyc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/85f322/32.png) [@interactivenyc](https://www.ruby-forum.com/u/interactivenyc)\
**Post date:** [April 17, 2009, 3:01am UTC](https://www.ruby-forum.com/t/storing-active-record-validation-code-in-the-database/165756/1 "2009-04-17T03:01:40Z")

</div>

In the book ‘The Rails Way’, Obie F. discusses keeping active-  
record validation code within a database and then applying those  
validations conditionally based upon certain attributes of the model.

I had a similar requirement recently, before reading the book and  
ended up writing my own hand-rolled validator. This was quite painful  
and not as powerful as the built in ActiveRecord validations.

Does any one have any info/links to posts etc that would give me a  
headstart on how to achieve conditional AR validations with the  
validation code stored in a database?

---

<div class="post-metadata">

**Author:** ![interactivenyc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/85f322/32.png) [@interactivenyc](https://www.ruby-forum.com/u/interactivenyc)\
**Post date:** [April 17, 2009, 6:52pm UTC](https://www.ruby-forum.com/t/storing-active-record-validation-code-in-the-database/165756/2 "2009-04-17T18:52:31Z")

</div>

I think perhaps I have not phrased this very well, and that explains  
the lack of replies.

I have a number of ActiveRecord models all based around an ‘Order  
Model’, a shortened version of which is shown below.

class Order \< ActiveRecord::Base

has\_many: previous\_addresses  
has\_one: applicant  
has\_one: current\_address  
end

All of the other models belong to order.

I wish to validate the various models such as applicant,  
current\_address etc differently based upon the company attribute of  
the order.

I have developed a solution to this which involved a complete hand-  
rolled validation suite, supporting all of the standard activeRecord  
validators. This solution was developed with a number of tables such  
as model, fields, field\_items etc that described the validation and  
amounts to quite a lot of data.

I now have to manipulate this data and copy and edit it for each new  
company that I am connecting to. This is tedious and error prone.  
Whilst reading ‘The Rails Way’, I found a section where Obie F.  
described keeping the validation code in the database in the form of  
ActiveRecord validation statements and injecting these into the models  
at runtime based upon the value of the company id.

I am imagining keeping data in tables such as the following :-

company: 200  
model: person  
Code validates\_length\_of :middle\_name, :maximum =\> 20, :allow\_nil =\>  
true

company: 201  
model: person  
Code validates\_length\_of :middle\_name, :maximum =\> 1, :allow\_nil =\>  
true

So for company 201 a middle name of ‘John’ would fail validation but  
that would be ok for company 200.

Has anyone seen this done, and if so do they have any tips/suggestions  
on how to do it, even better does anyone have any links to tutorials/  
code that does this?

---

<div class="post-metadata">

**Author:** ![interactivenyc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/85f322/32.png) [@interactivenyc](https://www.ruby-forum.com/u/interactivenyc)\
**Post date:** [April 17, 2009, 11:54pm UTC](https://www.ruby-forum.com/t/storing-active-record-validation-code-in-the-database/165756/3 "2009-04-17T23:54:01Z")

</div>

On Friday 17 April 2009, sweet61 wrote:

> I am imagining keeping data in tables such as the following :-

You know that you can call the ordinary validates\_… methods from  
another method?

class Company \< ActiveRecord::Base  
include DatabaseValidator  
end

module DatabaseValidator  
def self.included(base)  
.each do |vmethod, options|  
base.send(vmethod, options)  
end  
end  
end

You could store the validation method simply as the method name and the  
options as a YAML serialized hash.

Michael

–  
Michael S.  
[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
[http://www.schuerig.de/michael/](http://www.schuerig.de/michael/)
