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:
-
Why does the validation_uniqueness_of NOT use the value that is going
to be inserted into the database for its check? -
Is this a feature or a bug?
-
Where is this value passed to validate_uniqueness_of if not from the
model; from the controller? -
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