# using database for validation

**URL:** <https://discuss.rubyonrails.org/t/using-database-for-validation/17676>\
**Category:** rubyonrails-talk\
**Created:** [September 2, 2007, 2:09pm UTC](https://discuss.rubyonrails.org/t/using-database-for-validation/17676 "2007-09-02T14:09:51Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![dailer](https://avatars.discourse-cdn.com/v4/letter/d/13edae/32.png) [@dailer](https://discuss.rubyonrails.org/u/dailer)\
**Post date:** [September 2, 2007, 2:09pm UTC](https://discuss.rubyonrails.org/t/using-database-for-validation/17676/1 "2007-09-02T14:09:51Z")

</div>

I've been playing around with the idea of using the database for model validation where possible (instead of duplicating that validation in rails). My first approach to this was to catch exceptions from save/ create and parse the errors, in my case from postgres, to turn them into something friendlier. Something like:

&nbsp;&nbsp;def update &nbsp;&nbsp;&nbsp;&nbsp;create &nbsp;&nbsp;end

&nbsp;&nbsp;def create &nbsp;&nbsp;&nbsp;&nbsp;begin &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;super &nbsp;&nbsp;&nbsp;&nbsp;rescue ActiveRecord::StatementInvalid =\> e &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;errors.add\_to\_base(e.to\_s) &nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;end

but apparently rails ignores errors at this point. This just results in rails thinking the record is saved when it has not been. So next I tried something more elaborate. This is a bit contrived and I don't think I would actually use this but I'm just trying to figure out callbacks and such:

&nbsp;&nbsp;def initialize (attrs = nil) &nbsp;&nbsp;&nbsp;&nbsp;super(attrs) &nbsp;&nbsp;&nbsp;&nbsp;@attempt = 0 &nbsp;&nbsp;end

&nbsp;&nbsp;def update &nbsp;&nbsp;&nbsp;&nbsp;create &nbsp;&nbsp;end

&nbsp;&nbsp;def create &nbsp;&nbsp;&nbsp;&nbsp;begin &nbsp;&nbsp;&nbsp;&nbsp;@attempt += 1 &nbsp;&nbsp;&nbsp;&nbsp;if @attempt == 1 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@error = nil &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;super &nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;&nbsp;&nbsp;rescue ActiveRecord::StatementInvalid =\> e &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@error = e.to\_s &nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;end

&nbsp;&nbsp;def after\_save &nbsp;&nbsp;&nbsp;&nbsp;if !@error.nil? and @attempt \< 2 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;save! &nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;end

&nbsp;&nbsp;def validate &nbsp;&nbsp;&nbsp;&nbsp;if @error.nil? &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;super &nbsp;&nbsp;&nbsp;&nbsp;else &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;errors.add\_to\_base(@error) &nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;end

The idea was to catch the original exception and save it, force it back thru "save" and set the error in that second passes call to validate. Even though the call to "errors.add\_to\_base(@error)" works, it doesn't seem to prevent the exception being thrown in the UI.

Has anyone attempted something like this or have any ideas how to proceed? I guess I'm gonna have to dig into the rails code a bit deeper.
