Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #5896 (new defect)

Opened 2 years ago

Last modified 10 months ago

[PATCH] validates_uniqueness_of should use values before cast

Reported by: me@julik.nl Assigned to: David
Priority: high Milestone: 1.2.7
Component: ActiveRecord Version: edge
Severity: normal Keywords: validations uniqueness typecast
Cc: bitsweat, tarmo

Description

In case when serialization is used (or custom accessors are defined) AR should use the original SQL representation for doing uniqueness queries instead of calling the record's accessors.

This probably fixes #544 as well.

Attachments

validation_no_cast.diff (3.4 kB) - added by me@julik.nl on 08/24/06 02:38:48.
patch
uniq_with_adapters.diff (7.5 kB) - added by me@julik.nl on 08/24/06 02:47:02.
now with gazillion adapters (untested)
validates_uniqueness.diff (4.1 kB) - added by wildchild on 07/25/07 04:11:40.
Tested with trunk, updated db_definitions/schema.rb according to bitsweat.

Change History

08/24/06 02:38:48 changed by me@julik.nl

  • attachment validation_no_cast.diff added.

patch

08/24/06 02:47:02 changed by me@julik.nl

  • attachment uniq_with_adapters.diff added.

now with gazillion adapters (untested)

08/24/06 03:36:37 changed by anonymous

  • version changed from edge to 1.1.6.
  • milestone set to 1.x.

Could someone create a 1.1.6 patch please?

08/24/06 03:57:48 changed by bitsweat

  • cc set to bitsweat.

Please use test/fixtures/db_definitions/schema.rb instead of tweaking every SQL file. The patch looks good, otherwise.

08/24/06 04:11:06 changed by me@julik.nl

This doesn't apply because schema.rb only has the newest table definitions

I don't think this needs backporting to 1.1.6 and I don't like the private method call - but validations can be considered AR-internals I guess.

If the patch is OK please apply. I didn't see a proper place to stick an IP thing in the newest fixtures alas.

04/22/07 10:40:29 changed by wildchild

  • priority changed from normal to high.
  • severity changed from normal to major.
  • milestone changed from 1.x to 1.2.4.

07/25/07 04:11:40 changed by wildchild

  • attachment validates_uniqueness.diff added.

Tested with trunk, updated db_definitions/schema.rb according to bitsweat.

07/25/07 04:16:43 changed by wildchild

  • version changed from 1.1.6 to edge.
  • severity changed from major to normal.

All tests passed, please apply, sure patch is ok.

08/07/07 06:15:26 changed by tarmo

  • cc changed from bitsweat to bitsweat, tarmo.

Still applies cleanly, tests pass on mysql 5.0, postgresql 8.2, sqlite 2.8/3.4. +1

09/14/07 04:45:55 changed by bitsweat

This fixes one scenario and breaks another. Consider

class Foo < ActiveRecord::Base
  validates_uniqueness_of :computed_attribute

  def computed_attribute
    # ...
  end
end

where read_attribute is not at play.

09/14/07 05:37:23 changed by julik

Ok now we are getting practical. What shalt I do? Downcast the thing returned by computed_attribute back into something the database can consume? That's possible unless there is something very intrinsic returned from the method. Isn't it proper to ask the user to define computed_value_before_cast or something of that senseif he needs that exact combination?