# After\_validation question

**URL:** <https://www.ruby-forum.com/t/after-validation-question/77155>\
**Category:** Rails\
**Created:** [November 24, 2006, 8:54pm UTC](https://www.ruby-forum.com/t/after-validation-question/77155 "2006-11-24T20:54:31Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![binkly](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/b5e925/32.png) [@binkly](https://www.ruby-forum.com/u/binkly)\
**Post date:** [November 24, 2006, 8:54pm UTC](https://www.ruby-forum.com/t/after-validation-question/77155/1 "2006-11-24T20:54:31Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![binkly](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/b5e925/32.png) [@binkly](https://www.ruby-forum.com/u/binkly)\
**Post date:** [November 24, 2006, 10:01pm UTC](https://www.ruby-forum.com/t/after-validation-question/77155/2 "2006-11-24T22:01:08Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![binkly](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/b5e925/32.png) [@binkly](https://www.ruby-forum.com/u/binkly)\
**Post date:** [November 24, 2006, 9:58pm UTC](https://www.ruby-forum.com/t/after-validation-question/77155/3 "2006-11-24T21:58:33Z")

</div>

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’

---

<div class="post-metadata">

**Author:** ![binkly](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/b5e925/32.png) [@binkly](https://www.ruby-forum.com/u/binkly)\
**Post date:** [November 24, 2006, 10:12pm UTC](https://www.ruby-forum.com/t/after-validation-question/77155/4 "2006-11-24T22:12:42Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![binkly](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/b5e925/32.png) [@binkly](https://www.ruby-forum.com/u/binkly)\
**Post date:** [November 24, 2006, 10:01pm UTC](https://www.ruby-forum.com/t/after-validation-question/77155/5 "2006-11-24T22:01:45Z")

</div>

oh and if validation fails the after\_validation event will not fire
