# Where is Rails Validations Value Hash Initialization?

**URL:** <https://www.ruby-forum.com/t/where-is-rails-validations-value-hash-initialization/132373>\
**Category:** Rails\
**Created:** [March 19, 2008, 3:24pm UTC](https://www.ruby-forum.com/t/where-is-rails-validations-value-hash-initialization/132373 "2008-03-19T15:24:19Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![byrnejb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d2c977/32.png) [@byrnejb](https://www.ruby-forum.com/u/byrnejb)\
**Post date:** [March 19, 2008, 3:24pm UTC](https://www.ruby-forum.com/t/where-is-rails-validations-value-hash-initialization/132373/1 "2008-03-19T15:24:19Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![byrnejb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d2c977/32.png) [@byrnejb](https://www.ruby-forum.com/u/byrnejb)\
**Post date:** [March 19, 2008, 3:42pm UTC](https://www.ruby-forum.com/t/where-is-rails-validations-value-hash-initialization/132373/2 "2008-03-19T15:42:29Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![byrnejb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d2c977/32.png) [@byrnejb](https://www.ruby-forum.com/u/byrnejb)\
**Post date:** [March 19, 2008, 4:02pm UTC](https://www.ruby-forum.com/t/where-is-rails-validations-value-hash-initialization/132373/3 "2008-03-19T16:02:46Z")

</div>

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…
