Validate unless, LoginGenerator difficulty


#1

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. :smiley:


#2

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


#3

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


#4

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