# Test for field validation before saving

**URL:** <https://www.ruby-forum.com/t/test-for-field-validation-before-saving/180967>\
**Category:** Rails\
**Created:** [January 2, 2010, 12:00pm UTC](https://www.ruby-forum.com/t/test-for-field-validation-before-saving/180967 "2010-01-02T12:00:19Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![SSSSSlvaro\_Morais](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c57346/32.png) [@SSSSSlvaro\_Morais](https://www.ruby-forum.com/u/SSSSSlvaro_Morais)\
**Post date:** [January 2, 2010, 12:00pm UTC](https://www.ruby-forum.com/t/test-for-field-validation-before-saving/180967/1 "2010-01-02T12:00:19Z")

</div>

Hi. I’m new to rails, and I must say I’m loving it.  
I have a question that some of you may already found the answer.

Here’s the problem  
I’ve made a simple user signup interface that sends a confirmation  
link to the email of the link.  
The problem is that I want to confirm if everything is ok before  
saving including the sending of the confirmation email.

So:

So i have

@user.get\_confirmation # To populate the confirmation fields.  
if @user.save  
@user.send\_confirmation  
else  
…  
end

The problem is that @user is saved before the confirmation is sent so  
my only option is to display a message that there was a problem with  
the confirmation email, and delete the record after.

Is there a method like @user.save\_test  
so that I can make something like this:

@user.get\_confirmation  
if @user\_save\_test  
if @user.send\_confirmation  
@user.save  
end  
end

Thanks in advance.

–

You received this message because you are subscribed to the Google  
Groups “Ruby on Rails: Talk” group.  
To post to this group, send email to removed\_email\_address@domain.invalid.  
To unsubscribe from this group, send email to  
removed\_email\_address@domain.invalid.  
For more options, visit this group at  
[http://groups.google.com/group/rubyonrails-talk?hl=en](http://groups.google.com/group/rubyonrails-talk?hl=en).

---

<div class="post-metadata">

**Author:** ![SSSSSlvaro\_Morais](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c57346/32.png) [@SSSSSlvaro\_Morais](https://www.ruby-forum.com/u/SSSSSlvaro_Morais)\
**Post date:** [January 2, 2010, 12:07pm UTC](https://www.ruby-forum.com/t/test-for-field-validation-before-saving/180967/2 "2010-01-02T12:07:56Z")

</div>

On Fri, Jan 1, 2010 at 7:53 PM, Álvaro Morais [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> my only option is to display a message that there was a problem with  
> end

You should use a before\_save callback in your model.  
Something like this:

Class User  
def before\_save  
self.send\_confirmation if self.valid?  
end  
end

Also, you should take a look at:  
[http://guides.rubyonrails.org/activerecord\_validations\_callbacks.html](http://guides.rubyonrails.org/activerecord_validations_callbacks.html)

Hope it helps.

## Cheers.

Leonardo M…  
There’s no place like ~

–

You received this message because you are subscribed to the Google  
Groups “Ruby on Rails: Talk” group.  
To post to this group, send email to removed\_email\_address@domain.invalid.  
To unsubscribe from this group, send email to  
removed\_email\_address@domain.invalid.  
For more options, visit this group at  
[http://groups.google.com/group/rubyonrails-talk?hl=en](http://groups.google.com/group/rubyonrails-talk?hl=en).

---

<div class="post-metadata">

**Author:** ![SSSSSlvaro\_Morais](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c57346/32.png) [@SSSSSlvaro\_Morais](https://www.ruby-forum.com/u/SSSSSlvaro_Morais)\
**Post date:** [January 2, 2010, 1:25pm UTC](https://www.ruby-forum.com/t/test-for-field-validation-before-saving/180967/3 "2010-01-02T13:25:12Z")

</div>

Yes it helps. Thank you.  
Love your sig.

On Jan 2, 11:06 am, Leonardo M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > So:  
> > The problem is that @user is saved before the confirmation is sent so  
> > end
> 
> Also, you should take a look at:[http://guides.rubyonrails.org/activerecord\_validations\_callbacks.html](http://guides.rubyonrails.org/activerecord_validations_callbacks.html)
> 
> Hope it helps.
> 
> ## Cheers.
> 
> Leonardo M…  
> There’s no place like ~

–

You received this message because you are subscribed to the Google  
Groups “Ruby on Rails: Talk” group.  
To post to this group, send email to removed\_email\_address@domain.invalid.  
To unsubscribe from this group, send email to  
removed\_email\_address@domain.invalid.  
For more options, visit this group at  
[http://groups.google.com/group/rubyonrails-talk?hl=en](http://groups.google.com/group/rubyonrails-talk?hl=en).
