This project is archived and is in readonly mode.

#3484 ✓stale

[Rails 2.3.4 and prior] validates_uniqueness_of fails with non STI inheritance

Reported by Hery | November 12th, 2009 @ 10:00 AM

I've found this issue when using class inheritance but non-STI

class User < ActiveRecord::Base
  validates_uniqueness_of :login

class Student < User
  set_table_name :students

Just created 1 column login for both tables

users(:login, :null => false)
students(:login, :null => false)
u = User.create(:login => 'toto' )
s = Student.create(:login => 'toto')
no such column: students.login: SELECT "users".id FROM "users" WHERE ("students"."login" = 'toto')  LIMIT 1

Comments and changes to this ticket

  • Matt Jones

    Matt Jones November 15th, 2009 @ 10:04 PM

    What's the expected behavior in this instance? Should :login be unique across all records in the students table only, or across all records in users as well?

    The former would seem relatively straightforward (change the code that searches for a base class to exclude classes with a different table_name), while the latter would be extremely difficult.

  • Hery

    Hery November 15th, 2009 @ 10:18 PM

    IMHO the expected behavior is a unique login in the students table only.

    I know it is a bad design since Mixin should be used instead of inheritance but I just pointed out that this simple code raises a MysqlError.

  • Rohit Arondekar

    Rohit Arondekar October 6th, 2010 @ 06:36 AM

    • State changed from “new” to “stale”
    • Importance changed from “” to “”

    Marking ticket as stale. If this is still an issue please leave a comment with suggested changes, creating a patch with tests, rebasing an existing patch or just confirming the issue on a latest release or master/branches.

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href=""></a>