Forum: Ruby on Rails STI and validates_uniqueness

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Andy W. (Guest)
on 2007-04-20 03:02
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
Mark Reginald J. (Guest)
on 2007-04-20 05:34
(Received via mailing list)
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/...
Trunk:
http://dev.rubyonrails.org/browser/trunk/activerec...

removing "record.class", leaving a plain find.

Mark

--
We develop, watch us RoR, in numbers too big to ignore.
Andy W. (Guest)
on 2007-04-20 09:39
ThankYou!  Will give this a go.
This topic is locked and can not be replied to.