After_validation question

Hello,

Lets say you’re going to do validates_uniqueness_of on a model’s e-mail
address attribute.

If the e-mail address already exists how do I tell the model to just do
an update instead of redirecting back to the new/create page indicating
that the address already exists? I see a callback called
after_validation but I’m just not sure how to use it; it indeed it is
the method to use.

Any help would be much appreciated.

Thanks,

Binh

the more important question is why are you validating for uniquness if
you are going to update any way?

i find the simplist/DRYist way to do create/update is to use a define
method
in you application controller put the function

def define(object,get?,id,vals)
if id
model = Object.const_get(object).find(id)
else
model = Object.const_get(object).new
end

if not get?
if id
model.update_attributes(vals)
end
if model.save
flash[:note] = ‘’
else
flash[:note] = ‘’
end
end
return model
end

then in the child controller you call

@model = super(‘model name’, request.get?, params[:id], params[:model])

lastly you need to edit routs to say

map.connect ‘/:controller/edit/:id’, :action => ‘define’
map.connect ‘/:controller/create’, :action => ‘define’

hello keynan,

thanks for your response. perhaps i’m thinking about the problem in the
wrong way then. the scenario is, if a visitor comes back to the site
and sign up for a different event, i want to check to see if he’s
already in the DB, and if he is i’ll just update his record instead of
creating a new one. but, perhaps that shoudl be done at the controller
level instead of the model?

binh

Keynan P. wrote:

oh and if validation fails the after_validation event will not fire

oh and if validation fails the after_validation event will not fire