# Validate unless, LoginGenerator difficulty

**URL:** <https://www.ruby-forum.com/t/validate-unless-logingenerator-difficulty/62135>\
**Category:** Rails\
**Created:** [May 31, 2006, 8:45pm UTC](https://www.ruby-forum.com/t/validate-unless-logingenerator-difficulty/62135 "2006-05-31T20:45:18Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Julie\_Fuller](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b9bd4f/32.png) [@Julie\_Fuller](https://www.ruby-forum.com/u/Julie_Fuller)\
**Post date:** [May 31, 2006, 8:45pm UTC](https://www.ruby-forum.com/t/validate-unless-logingenerator-difficulty/62135/1 "2006-05-31T20:45:18Z")

</div>

I apologize if this is a very simple question; I’ve searched the forums,  
wiki, Google, and Agile Web D. with Rails to no avail.

Is there a way to put conditional validation in the model? For  
instance, if I want to limit items a user can add unless the user is an  
admin? My current code is:

validates\_uniqueness\_of :user\_id, :scope =\> ‘event\_id’,  
:message =\> ‘You have already uploaded an item for this event.’

And I need to add something that overrides that requirement if  
user.account\_type == ‘admin’.

I’m also running into a problem where I need my admin users to be able  
to edit other user’s information (and items) without becoming the new  
“owner” of that item/user. I think this is a conceptual problem on my  
part, but if someone could give me a pointer as to _how you have two  
instances of the same model open_ (the current user and the user being  
edited) it would be much appreciated. I’m having the same problem with  
a search box that appears on the same page as an individual item – even  
if I hard-code the search form values in as blank, Rails is overwriting  
it and filling the current item’s values in the search box. But I can’t  
just rename the fields because they’re what’s used for search!

Thank you in advance for any assistance. 😃

---

<div class="post-metadata">

**Author:** ![Julie\_Fuller](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b9bd4f/32.png) [@Julie\_Fuller](https://www.ruby-forum.com/u/Julie_Fuller)\
**Post date:** [May 31, 2006, 9:19pm UTC](https://www.ruby-forum.com/t/validate-unless-logingenerator-difficulty/62135/2 "2006-05-31T21:19:33Z")

</div>

> Is there a way to put conditional validation in the model? For  
> instance, if I want to limit items a user can add unless the user  
> is an  
> admin? My current code is:
> 
> validates\_uniqueness\_of :user\_id, :scope =\> ‘event\_id’,  
> :message =\> ‘You have already uploaded an item for this event.’
> 
> And I need to add something that overrides that requirement if  
> user.account\_type == ‘admin’.

You can put an :if option on validates\_uniqueness\_of so it’ll only  
validate if that turns out to be true, so you could say: (message and  
scope removed for brevity)

validates\_uniqueness\_of :user\_id, :if =\> :not\_admin?

and have this method:

def not\_admin?  
account\_type != ‘admin’  
end

Unfortunately there’s no :unless option right now. You could also use  
a proc object to avoid a perhaps useless “not\_admin?” method:

validates\_uniqueness\_of :user\_id, :if =\> Proc.new {|user|  
user.account\_type != ‘admin’}

–  
Michael D.  
[http://www.mdaines.com](http://www.mdaines.com)

---

<div class="post-metadata">

**Author:** ![Julie\_Fuller](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b9bd4f/32.png) [@Julie\_Fuller](https://www.ruby-forum.com/u/Julie_Fuller)\
**Post date:** [June 1, 2006, 6:30pm UTC](https://www.ruby-forum.com/t/validate-unless-logingenerator-difficulty/62135/3 "2006-06-01T18:30:18Z")

</div>

> You can put an :if option on validates\_uniqueness\_of so it’ll only  
> validate if that turns out to be true, so you could say: (message and  
> scope removed for brevity)
> 
> validates\_uniqueness\_of :user\_id, :if =\> :not\_admin?
> 
> and have this method:
> 
> def not\_admin?  
> account\_type != ‘admin’  
> end

Actually, I can’t get that to work, because the model won’t recognize my  
session variable – @session[‘user’].id results in a “You have a nil  
object when you didn’t expect it!” error. I’ve tried everything I can  
think of, and regardless of what my session object is, or how I call it,  
it won’t come up… can you use sessions in the middle of a model? Or  
only a controller?

I’m relatively new to Ruby… sorry if these are dumb questions, but I’m  
having trouble finding information – if I Google the error messages I  
get, nothing comes up. :-\

Julie

---

<div class="post-metadata">

**Author:** ![Julie\_Fuller](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b9bd4f/32.png) [@Julie\_Fuller](https://www.ruby-forum.com/u/Julie_Fuller)\
**Post date:** [May 31, 2006, 10:55pm UTC](https://www.ruby-forum.com/t/validate-unless-logingenerator-difficulty/62135/4 "2006-05-31T22:55:02Z")

</div>

> You can put an :if option on validates\_uniqueness\_of so it’ll only  
> validate if that turns out to be true, so you could say: (message and  
> scope removed for brevity)  
> Thank you! That’s exactly what I was looking for. 🙂 Oddly I can’t  
> find it in the wiki or in the book even though I know what I’m looking  
> for now.

Does anyone have an answer for my other question, regarding having two  
rows of the same model open at the same time without them interfering  
with one another?

Thank you!

Julie
