# Validate\_presence\_of strange behaviour

**URL:** <https://www.ruby-forum.com/t/validate-presence-of-strange-behaviour/140985>\
**Category:** Rails\
**Created:** [June 20, 2008, 2:27pm UTC](https://www.ruby-forum.com/t/validate-presence-of-strange-behaviour/140985 "2008-06-20T14:27:21Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![elmy-liddiard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/c67d28/32.png) [@elmy-liddiard](https://www.ruby-forum.com/u/elmy-liddiard)\
**Post date:** [June 20, 2008, 2:27pm UTC](https://www.ruby-forum.com/t/validate-presence-of-strange-behaviour/140985/1 "2008-06-20T14:27:21Z")

</div>

Folks,

I need some help to understand some strange behaviour when using  
validates\_presence\_of

I have a form that updates table row. One field is text and I require it  
to be completed.

With no validation in place in the model I can obvioulsy update the row  
and either put a text entry in or leave it blank and the update happens  
okay.

After adding validate\_presence\_of :activity to the model …  
I then update with the text item completed and it updates okay.  
but if I then update with the text item null I get what appears to be a  
totally unrelated error:-

“Called id for nil, which would mistakenly be 4 – if you really wanted  
the id of nil, use object\_id” and which points to the code line:-

Of course what I would expect is to get an “Activity must be completed”  
error.

So when I would expect validate\_presence\_of to trigger a real validation  
error it in fact throws up this other condition.

I suspect the actual error may be unrelated to validate\_presence\_of but  
that the validate routine is merely exposing some other underlying issue  
and without the validation in place it all works okay and I dont have  
any issues or errors shown!

Confused since the validate\_presence\_of should be so simple and yet it  
throws up this other condition.

Can anyone give me a steer as to what/where I should be investigating  
this.

Any help much appreciated!

---

<div class="post-metadata">

**Author:** ![elmy-liddiard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/c67d28/32.png) [@elmy-liddiard](https://www.ruby-forum.com/u/elmy-liddiard)\
**Post date:** [June 20, 2008, 2:42pm UTC](https://www.ruby-forum.com/t/validate-presence-of-strange-behaviour/140985/2 "2008-06-20T14:42:12Z")

</div>

You get an error in this line

> 

That has nothing to do with the validation as such.  
You try to get @activities.id which will fail with the error you  
mentioned if it’s nil  
So the problem is wherever you do  
@activities = …

Why this doesn’t fail without the validation is another question

By the way:  
You use activities here, which is plural and should contain a list  
rather then an object.  
For easy code reading it would be better to be clear about that  
point and either use @activity if it’s a singular object or your  
code is wrong to try to get an id from it  
(But can’t say that for sure without the rest of your code)
