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