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