Where is Rails Validations Value Hash Initialization?

I have run into a problem with Rails “validates_uniqueness_of”. The
evidence indicates that this method does not get attribute values set in
the model but rather uses raw data passed from the view. I have traced
through the process with the debugger but my knowledge of rails
internals is too sketchy for me to pinpoint where and how the value
initialization is taking place.

The symptom of the difficulty is demonstrated below. Given an input
string for @entity.entity_name of “A TEST CLIENT” the model invokes
an override on the default accessors to produce a final string of
entity.entity_name = “a test client”. The validates_uniqueness_of
:entity_name for the above situation passes even if “a test client” is
already on file but the subsequent SQL INSERT statement generated fails
because the value passed is “a test client” and this triggers an SQL
exception because of an INDEX UNIQUE constraint on that column.

My questions are these:

  1. Why does the validation_uniqueness_of NOT use the value that is going
    to be inserted into the database for its check?

  2. Is this a feature or a bug?

  3. Where is this value passed to validate_uniqueness_of if not from the
    model; from the controller?

  4. Do I have to replicate model code in the controller to achieve the
    effect desired, a validation check using the values that will be used
    for insertion?

X-Posted to WWR-Forum

I found the source of my confusion. I had redefined the getter method
for entity_name to provide a titlecase display to the view. It is this
value that the validates_uniqueness_of is using, which is different than
the value provided from the overloaded setter method (keycase).

I am still not sure why the validates would use a value from one source
for validation and a value from another source for the actual update but
at least I can now get this to work.

James B. wrote:

I am still not sure why the validates would use a value from one source
for validation and a value from another source for the actual update but
at least I can now get this to work.

Evidently, I was up too late chasing this down and did not have enough
coffee this morning to wake me. Of course the validator is going to
take the value from the attribute getter and of course it is my job to
make sure that the getter and setter say the same things.

Sigh…