After_validation question


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.



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
in you application controller put the function

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

if not get?
if id
flash[:note] = ‘’
flash[:note] = ‘’
return model

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?


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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs