Single Table Inheritance validates_uniqueness_of Problem


Consider a case of STI where:

Now try following at console:

This will let you create three records in users table with same name, validates_uniqueness_of written in User class has no effect on it. validates_uniqueness_of automatically scoped with class names, that means it will not let you create two managers with same name or two customers with same name or two users with same name.

If you want uniqueness of an attribute in overall table, put the following code in some file in your lib dir and require that file in environment:

And then use validates_overall_uniqueness_of instead of validates_uniqueness_of.

Information and Links

Join the fray by commenting, tracking what others have to say, or linking to it from your blog.


Other Posts
Two best online API/Rails API
Running Rails Application on https with pound

Reader Comments

When you define methods this way, there is no such problem:

class User

Thanks,
I got this error and your solution worked here.

You are the man

Write a Comment

Take a moment to comment and tell us what you think. Some basic HTML is allowed for formatting.