STI and validates_uniqueness


#1

Hi,

Added STI and it’s messed with my validates_uniqueness.
I was hoping a validate_uniqueness in the base class would validate a
field regardless of the derived classes…but it seems not to be.

Any ideas how I can enforce the below uniqueness across all inherited
classes?
validate_uniqueness_of :xyz_id , :scope => “user_id”

Only thing I can think of is trying before_saves and before_updates
somewhere and including them in each inherited class definition.

Many Thanks
Andy


#2

Andy W. wrote:

Added STI and it’s messed with my validates_uniqueness.
I was hoping a validate_uniqueness in the base class would validate a
field regardless of the derived classes…but it seems not to be.

Any ideas how I can enforce the below uniqueness across all inherited
classes?
validate_uniqueness_of :xyz_id , :scope => “user_id”

This is an as yet unfixed problem with Active Record

 http://dev.rubyonrails.org/ticket/3833
 http://dev.rubyonrails.org/ticket/5303

The quick fix I use is to change the following line

1.2.3:
http://dev.rubyonrails.org/browser/tags/rel_1-2-3/activerecord/lib/active_record/validations.rb#L551
Trunk:
http://dev.rubyonrails.org/browser/trunk/activerecord/lib/active_record/validations.rb#L613

removing “record.class”, leaving a plain find.

Mark


We develop, watch us RoR, in numbers too big to ignore.


#3

ThankYou! Will give this a go.