# Different validation for different users

**URL:** <https://www.ruby-forum.com/t/different-validation-for-different-users/57081>\
**Category:** Rails\
**Created:** [April 7, 2006, 2:05am UTC](https://www.ruby-forum.com/t/different-validation-for-different-users/57081 "2006-04-07T02:05:35Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![lantis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/4da419/32.png) [@lantis](https://www.ruby-forum.com/u/lantis)\
**Post date:** [April 7, 2006, 2:05am UTC](https://www.ruby-forum.com/t/different-validation-for-different-users/57081/1 "2006-04-07T02:05:35Z")

</div>

Hi,

I have two types of users; normal user and admin.  
I have a field called priority in the table task.  
This priority field is invisible to the normal user, but not to the  
admin. The admin has to fill in the priority field when he adds a record  
via filling a form. Thus, i need to add validates\_presence\_of :priority  
to the model task. But, if i do this, then the normal user can’t add a  
record to the task as he will fail the validation.

One way i can think of is for the normal user controller to fill in the  
priority value before the record is saved to pass the validation. Then,  
the priority field is edited to null / empty after the record has been  
saved saved.

I dont like this solution. What’s a better way of doing this?

Thanks,

Lantis.

---

<div class="post-metadata">

**Author:** ![lantis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/4da419/32.png) [@lantis](https://www.ruby-forum.com/u/lantis)\
**Post date:** [April 7, 2006, 2:20am UTC](https://www.ruby-forum.com/t/different-validation-for-different-users/57081/2 "2006-04-07T02:20:30Z")

</div>

Some of the user management frameworks add a method such as  
User.current\_user to be able to easily get the currently logged in user.  
If  
you have that you’re problem solves itself:

validates\_presence\_of :priority, :if =\> Proc.new {  
User.current\_user.admin?}

See the following link for an implementation of User.current\_user

[http://livsey.org/2005/07/16/adding\_created\_by\_and\_updated\_by\_to\_rails/](http://livsey.org/2005/07/16/adding_created_by_and_updated_by_to_rails/)

IMHO it makes more sense to call it User.current than User.current\_user  
but  
it’s not a big deal.

-Jonathan.

---

<div class="post-metadata">

**Author:** ![lantis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/4da419/32.png) [@lantis](https://www.ruby-forum.com/u/lantis)\
**Post date:** [April 7, 2006, 5:28pm UTC](https://www.ruby-forum.com/t/different-validation-for-different-users/57081/3 "2006-04-07T17:28:00Z")

</div>

Jonathan V. wrote:

> Some of the user management frameworks add a method such as  
> User.current\_user to be able to easily get the currently logged in user.  
> If  
> you have that you’re problem solves itself:
> 
> validates\_presence\_of :priority, :if =\> Proc.new {  
> User.current\_user.admin?}
> 
> See the following link for an implementation of User.current\_user
> 
> [http://livsey.org/2005/07/16/adding\_created\_by\_and\_updated\_by\_to\_rails/](http://livsey.org/2005/07/16/adding_created_by_and_updated_by_to_rails/)
> 
> IMHO it makes more sense to call it User.current than User.current\_user  
> but  
> it’s not a big deal.
> 
> -Jonathan.

Noob question:

why do i get “undefined method `current\_user’ for User:Class” error if i  
remove Proc.new?

Thanks,

Lantis.

---

<div class="post-metadata">

**Author:** ![lantis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/4da419/32.png) [@lantis](https://www.ruby-forum.com/u/lantis)\
**Post date:** [April 7, 2006, 11:24pm UTC](https://www.ruby-forum.com/t/different-validation-for-different-users/57081/4 "2006-04-07T23:24:46Z")

</div>

Lantis S. wrote:

> why do i get “undefined method `current\_user’ for User:Class” error if i  
> remove Proc.new?
> 
> Thanks,
> 
> Lantis.

I still don’t get his bit. Could anyone kindly explain please? 🙂
