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.

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

#1227

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
end

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

>> foo.save
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).

Reported by Nathaniel Bibler · October 16th, 2008 @ 04:32 PM

State: wontfix
Milestone: 2.x
Assigned to: nobody
Importance: none