# Validations and STI

**URL:** <https://www.ruby-forum.com/t/validations-and-sti/139029>\
**Category:** Rails\
**Created:** [May 30, 2008, 6:50am UTC](https://www.ruby-forum.com/t/validations-and-sti/139029 "2008-05-30T06:50:07Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![jwigal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/6a8cbe/32.png) [@jwigal](https://www.ruby-forum.com/u/jwigal)\
**Post date:** [May 30, 2008, 6:50am UTC](https://www.ruby-forum.com/t/validations-and-sti/139029/1 "2008-05-30T06:50:07Z")

</div>

The functionality with callbacks and STI does not seem to be working as  
expected. Here is a scenario and what I’ve done. Suggestion welcome.

assume you have parent.rb

class Parent \< ActiveRecord::Base  
validates\_uniqueness\_of :some\_field, :scope =\> :field\_1

before\_validation :update\_name

def update\_name  
self.name = “I’m a parent”  
end  
end

class Child \< Parent  
validates\_uniqueness\_of :some\_field, :scope =\> [:field\_1, :field\_2]

def update\_name  
self.name = “I’m a child”  
end  
end

now, one would expect that like the rest of inheritance that the child  
validates\_uniqueness\_of would as it does with callback methods. This  
does  
not work, however. You do correctly get parents named “I’m a parent” and  
children named “I’m a child” but children will still be validated  
according  
to the parent validation rules.

if you try this in parent:

validates\_uniqueness\_of :some\_field, :scope =\> :field\_1, :unless =\>  
self.is\_a?(Child)

this is even worse b/c now the callbacks are not called in this case if  
you  
are a child. All the callbacks - not just for the validation in  
question.

I guess what I would expect is that whatever is in the child objects in  
the  
inheritance structure would take priority. This works with callbacks.  
Even  
further I might want to define a validation at the parent that I don’t  
need  
at the child. The only solution in this case is to validate only at the  
child classes where you need that validation and leave the parent as  
open as  
possible.

Any ideas before I just throw this all in a callback method and do it  
the  
old way?

Thanks,

Jeff

–

Jeff W.  
removed\_email\_address@domain.invalid  
[http://boowebb.com/](http://boowebb.com/)
