Forum: Ruby on Rails Different validation for different users

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
15977b0a3b7e270bf3d1baaadfb79a59?d=identicon&s=25 Lantis (Guest)
on 2006-04-07 02:05
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.
631d1677786f0725da6ccee46043686e?d=identicon&s=25 Jonathan Viney (Guest)
on 2006-04-07 02:20
(Received via mailing list)
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...

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

-Jonathan.
15977b0a3b7e270bf3d1baaadfb79a59?d=identicon&s=25 Lantis Sephiro (lantis)
on 2006-04-07 17:28
Jonathan Viney 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...
>
> 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.
15977b0a3b7e270bf3d1baaadfb79a59?d=identicon&s=25 Lantis Sephiro (lantis)
on 2006-04-07 23:24
Lantis Sephiro 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? :)
This topic is locked and can not be replied to.