This project is archived and is in readonly mode.

#5494 ✓stale
Adam Meehan

Uniqueness validator can cause error with after_initialize callback

Reported by Adam Meehan | August 30th, 2010 @ 04:59 AM

The uniqueness validator uses the exists? method as the test for a duplicate record. The exists? method calls the find_by_sql method which will instantiate any record found. However the instantiated record will only have the primary attribute method defined because it is the only column in the select.

Given that the record is actually instantiated, any after_initialize callbacks will be run. If the an attribute method other than the primary key is called, then an ActiveModel::MissingAttribute error is raised. There seems to be an exception where the attribute method is called with the self receiver. Otherwise the error will occur.

I have attached a patch which uses 'count > 0' as the duplicate record test and therefore no record is instantiated and the callbacks are not run. This should also make it a little faster when a duplicate is found.

Perhaps a wider issue is whether the exists? method needs to do anything other than a count as well.

Comments and changes to this ticket

  • Adam Meehan

    Adam Meehan September 10th, 2010 @ 04:54 AM

    • Tag changed from activerecord, patch, validates_uniqueness_of, validations to activerecord, patch, performance, validates_uniqueness_of, validations
  • Kane

    Kane September 10th, 2010 @ 05:24 PM

    I encountered this too and monkey patched it in my app.

    I used STI and wanted to deny instantiation of the base class (kind of abstract).
    This class had a validates_uniqueness defined.

    Your diff is more than you wanted.

    Also i would suggest to fix the underlying problem, which is 'exists?' .
    there is no need for instantiation.

  • Marcelo Giorgi
  • Adam Meehan

    Adam Meehan September 10th, 2010 @ 11:15 PM

    Whoops, thanks for the heads up on the patch. Fixed now.

    A change to exists? caused too many errors to fix now. Will revisit.

  • Santiago Pastorino

    Santiago Pastorino February 2nd, 2011 @ 04:31 PM

    • State changed from “new” to “open”

    This issue has been automatically marked as stale because it has not been commented on for at least three months.

    The resources of the Rails core team are limited, and so we are asking for your help. If you can still reproduce this error on the 3-0-stable branch or on master, please reply with all of the information you have about it and add "[state:open]" to your comment. This will reopen the ticket for review. Likewise, if you feel that this is a very important feature for Rails to include, please reply with your explanation so we can consider it.

    Thank you for all your contributions, and we hope you will understand this step to focus our efforts where they are most helpful.

  • Santiago Pastorino

    Santiago Pastorino February 2nd, 2011 @ 04:31 PM

    • State changed from “open” to “stale”
  • Kane

    Kane March 29th, 2011 @ 03:03 PM

    Changeset [827e5de60f27c6b559640eda7b79857050a72d9e] by Andrew White

    exists? no longer instantiates records, so this should be solved.

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>

People watching this ticket