# Validate\_presence\_of :user or :user\_id?

**URL:** <https://www.ruby-forum.com/t/validate-presence-of-user-or-user-id/165698>\
**Category:** Rails\
**Created:** [April 16, 2009, 3:09pm UTC](https://www.ruby-forum.com/t/validate-presence-of-user-or-user-id/165698 "2009-04-16T15:09:41Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [April 16, 2009, 3:09pm UTC](https://www.ruby-forum.com/t/validate-presence-of-user-or-user-id/165698/1 "2009-04-16T15:09:41Z")

</div>

Hi all

I’m a bit confused whether it’s better to check for the ID of a related  
object or the related object itself?

validates\_presence\_of :user # is this better?  
validates\_presence\_of :user\_id # or is this better?

What would you suggest? Thanks,  
Josh

---

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [April 16, 2009, 3:24pm UTC](https://www.ruby-forum.com/t/validate-presence-of-user-or-user-id/165698/2 "2009-04-16T15:24:57Z")

</div>

validates\_presence\_of :user\_id  
will test if there is a value for self.user\_id regardless of whether  
there is a valid user with that id on the database.

If you’re going to use :user\_id then you should include  
validates\_associated :user to check the user is valid

validates\_presence\_of :user will check that the associated user is a  
valid record.

On Apr 16, 2:09 pm, Joshua M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [April 16, 2009, 3:28pm UTC](https://www.ruby-forum.com/t/validate-presence-of-user-or-user-id/165698/3 "2009-04-16T15:28:50Z")

</div>

Thanks for your explanation. 🙂

> validates\_presence\_of :user will check that the associated user is a  
> valid record.

so if the user id is nil, then there is no error?

---

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [April 16, 2009, 5:08pm UTC](https://www.ruby-forum.com/t/validate-presence-of-user-or-user-id/165698/4 "2009-04-16T17:08:41Z")

</div>

Gavin wrote:

> validates\_presence\_of :user\_id only tests that there is a value for  
> user\_id
> 
> with this method, user\_id could be any integer, not necessarily a  
> valid user.
> 
> validates\_presence\_of :user will also check that the user is a valid  
> record on the database.
> 
> That clear it up?

so validates\_presence\_of :user does the same like the combination of  
validates\_presence\_of :user\_id and validates\_associated :user… right?  
thanks for the explanation. 🙂

---

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [April 16, 2009, 3:35pm UTC](https://www.ruby-forum.com/t/validate-presence-of-user-or-user-id/165698/5 "2009-04-16T15:35:30Z")

</div>

validates\_presence\_of :user\_id only tests that there is a value for  
user\_id

with this method, user\_id could be any integer, not necessarily a  
valid user.

validates\_presence\_of :user will also check that the user is a valid  
record on the database.

That clear it up?
