# Excluding a user from the validate\_presence of

**URL:** <https://www.ruby-forum.com/t/excluding-a-user-from-the-validate-presence-of/115516>\
**Category:** Rails\
**Created:** [September 25, 2007, 11:16pm UTC](https://www.ruby-forum.com/t/excluding-a-user-from-the-validate-presence-of/115516 "2007-09-25T23:16:08Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![oweism](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/898d66/32.png) [@oweism](https://www.ruby-forum.com/u/oweism)\
**Post date:** [September 25, 2007, 11:16pm UTC](https://www.ruby-forum.com/t/excluding-a-user-from-the-validate-presence-of/115516/1 "2007-09-25T23:16:08Z")

</div>

hi,  
I am creating a website for a summer institute; i have flaged the  
student with a zero entry in the table, and the administrator with a  
one. An admin can edit a students information. However, i don’t want him  
to edit the password as soon as he click on edit list.  
I am trying to allow the admin of editing the student’s information  
without changing the password.  
my edit function is the default scaffold one.  
regards

---

<div class="post-metadata">

**Author:** ![oweism](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/898d66/32.png) [@oweism](https://www.ruby-forum.com/u/oweism)\
**Post date:** [September 25, 2007, 11:35pm UTC](https://www.ruby-forum.com/t/excluding-a-user-from-the-validate-presence-of/115516/2 "2007-09-25T23:35:34Z")

</div>

There are other ways to verify this, but I use the acts\_as\_modified  
plugin for similar things and it’s really handy.

[https://rubyforge.org/projects/actsasmodified](https://rubyforge.org/projects/actsasmodified)

With this you can do this in validate

def validate  
errors.add(:password, “You can not change a users password”) if  
self.modified? && self.password\_modified?  
end

Mouhannad Oweism wrote:

> hi,  
> I am creating a website for a summer institute; i have flaged the  
> student with a zero entry in the table, and the administrator with a  
> one. An admin can edit a students information. However, i don’t want him  
> to edit the password as soon as he click on edit list.  
> I am trying to allow the admin of editing the student’s information  
> without changing the password.  
> my edit function is the default scaffold one.  
> regards

–  
Sincerely,

William P.

---

<div class="post-metadata">

**Author:** ![oweism](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/898d66/32.png) [@oweism](https://www.ruby-forum.com/u/oweism)\
**Post date:** [September 26, 2007, 12:17am UTC](https://www.ruby-forum.com/t/excluding-a-user-from-the-validate-presence-of/115516/3 "2007-09-26T00:17:24Z")

</div>

Dear william,  
thank you for your quick response  
This might work if i want to prevent the admin and the user to edit the  
password. However, i want the admin not to have to modify the password  
if he wants to edit a user’s information.  
regards

---

<div class="post-metadata">

**Author:** ![oweism](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/898d66/32.png) [@oweism](https://www.ruby-forum.com/u/oweism)\
**Post date:** [September 26, 2007, 12:17am UTC](https://www.ruby-forum.com/t/excluding-a-user-from-the-validate-presence-of/115516/4 "2007-09-26T00:17:29Z")

</div>

After reading your post again, you may need to add another condition to  
the if statement to only disallow admins, like this:

def validate  
errors.add(:password, “You can not change a users password”) if  
current\_user.admin && self.modified? && self.password\_modified?  
end

This assumes you have a current\_user function to get at the current user  
and that admin is a method that returns a boolean

William P. wrote:

> end
> 
> > my edit function is the default scaffold one.  
> > regards

–  
Sincerely,

William P.

---

<div class="post-metadata">

**Author:** ![oweism](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/898d66/32.png) [@oweism](https://www.ruby-forum.com/u/oweism)\
**Post date:** [September 26, 2007, 12:20am UTC](https://www.ruby-forum.com/t/excluding-a-user-from-the-validate-presence-of/115516/5 "2007-09-26T00:20:16Z")

</div>

it is also worth mentioning william that the edit page is the same as  
the sign\_up page, and on that page i have used the validate\_presence\_of.  
thank u again

---

<div class="post-metadata">

**Author:** ![oweism](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/898d66/32.png) [@oweism](https://www.ruby-forum.com/u/oweism)\
**Post date:** [September 26, 2007, 12:34am UTC](https://www.ruby-forum.com/t/excluding-a-user-from-the-validate-presence-of/115516/6 "2007-09-26T00:34:27Z")

</div>

Ok, maybe I misunderstood you. Are you saying that when he edits the  
user, and leaves the password fields blank, you don’t want the password  
in the database to get wiped out? If thats the case, delete it from the  
params hash before calling update\_attributes if it’s empty

params[:user].delete(:password) if params[:user][:password].empty?  
@user = User.update(params[:id], params[:user])

That will not update the password field if that is what you are looking  
to do.

Mouhannad Oweism wrote:

> Dear william,  
> thank you for your quick response  
> This might work if i want to prevent the admin and the user to edit the  
> password. However, i want the admin not to have to modify the password  
> if he wants to edit a user’s information.  
> regards

–  
Sincerely,

William P.
