Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

Uniqueness validator can cause error with after_initialize callback

#5494

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.

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

State: stale
Milestone: none
Assigned to: nobody
Importance: none

Activity

  1. Adam Meehan
    Adam Meehan
    • Tag changed from activerecord, patch, validates_uniqueness_of, validations to activerecord, patch, performance, validates_uniqueness_of, validations

    September 10th, 2010 @ 04:54 AM

  2. Kane
    Kane

    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.

    September 10th, 2010 @ 05:24 PM

  3. Marcelo Giorgi
  4. Adam Meehan
    Adam Meehan

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

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

    September 10th, 2010 @ 11:15 PM

  5. Santiago Pastorino
    Santiago Pastorino
    • 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.

    February 2nd, 2011 @ 04:31 PM

  6. Santiago Pastorino
    Santiago Pastorino
    • State changed from open to stale

    February 2nd, 2011 @ 04:31 PM

  7. Kane
    Kane

    Changeset [827e5de60f27c6b559640eda7b79857050a72d9e] by Andrew White

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

    March 29th, 2011 @ 03:03 PM