Different validation for different users

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.

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/

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

-Jonathan.

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/

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.

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? :slight_smile: