This project is archived and is in readonly mode.

#1227 ✓wontfix
Nathaniel Bibler

Validations should not run when object isn't to be saved

Reported by Nathaniel Bibler | October 16th, 2008 @ 04:32 PM | in 2.x

This mainly has to do with the "new" dirty objects functionality in Rails (changeset 9127, March 29, 2008). Given the following test case:

class Foo < ActiveRecord::Base
  validates_uniqueness_of :name

Validation runs every time save is called, regardless of whether or not the object is actually dirty and to-be-saved:

>> foo = Foo.create!(:name => "Bar")
SQL> Foo Exists (0.3ms)   SELECT "foos".id FROM "foos" WHERE ("foos"."name" = 'Bar') LIMIT 1
SQL> Foo Create (0.5ms)   INSERT INTO "foos" ("name", "updated_at", "created_at") VALUES('Bar', '2008-10-16 15:27:47', '2008-10-16 15:27:47')
=> #<Foo id: 2, name: "Bar", created_at: "2008-10-16 15:26:47", updated_at: "2008-10-16 15:26:47">

>> foo.changed?
=> false

SQL> Foo Exists (0.3ms)   SELECT "foos".id FROM "foos" WHERE ("foos"."name" = 'Bar' AND "foos".id <> 3) LIMIT 1
=> true

You can see, above, that the object validations were run even though the object was not modified, save was not forced, and nothing was actually written to the database in the end.

This is less a bug than an area for potential performance improvement in the current code base. This behavior can be seen since inception of the dirty object functionality up to and through the most recent Git commit (517bc500ed95a84fd2aadff34fdc14cb7965bc6b).

Comments and changes to this ticket

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