# Modeling 'phony' fields

**URL:** <https://www.ruby-forum.com/t/modeling-phony-fields/65169>\
**Category:** Rails\
**Created:** [August 16, 2006, 5:05pm UTC](https://www.ruby-forum.com/t/modeling-phony-fields/65169 "2006-08-16T17:05:47Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![DuzenburyS\_Rich](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/43a26b/32.png) [@DuzenburyS\_Rich](https://www.ruby-forum.com/u/DuzenburyS_Rich)\
**Post date:** [August 16, 2006, 5:05pm UTC](https://www.ruby-forum.com/t/modeling-phony-fields/65169/1 "2006-08-16T17:05:47Z")

</div>

Hi,

I have a user model with an e-mail field. When a user registers for the  
site, I want them to confirm their email address by typing it in again.  
If they change their email address, I want them to confirm their email  
address by typing it in again. However, the user might change things on  
their account not related to their email address, such as their phone  
number, and they should not have to confirm their email address.

I suppose I can add an email\_confirmation field to the database, and  
then use

validates\_confirmation\_of :email

But, this seems rather wasteful and silly to store both the email and  
the email\_confirmation. I understand that the model need not store the  
field in the database, but then in that case User.find(:first) …  
fiddle with attributes other than email address … User.save (rightly)  
throws an error because the email\_confirmation field doesn’t match  
email.

What is the proper way to model a phony email\_confirmation field, such  
that confirmation validation is only fired when the e-mail address is  
actually updated?

Thank you.

Regards,

Rich

---

<div class="post-metadata">

**Author:** ![DuzenburyS\_Rich](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/43a26b/32.png) [@DuzenburyS\_Rich](https://www.ruby-forum.com/u/DuzenburyS_Rich)\
**Post date:** [August 16, 2006, 5:23pm UTC](https://www.ruby-forum.com/t/modeling-phony-fields/65169/2 "2006-08-16T17:23:49Z")

</div>

You don’t need to make a database field for the email\_confirmation,  
you can use it simply by including:

validates\_confirmation\_of :email

Rails handles the creation of a virtual attribute for you.

---

<div class="post-metadata">

**Author:** ![DuzenburyS\_Rich](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/43a26b/32.png) [@DuzenburyS\_Rich](https://www.ruby-forum.com/u/DuzenburyS_Rich)\
**Post date:** [August 16, 2006, 5:59pm UTC](https://www.ruby-forum.com/t/modeling-phony-fields/65169/3 "2006-08-16T17:59:58Z")

</div>

> What is the proper way to model a phony email\_confirmation  
> field, such that confirmation validation is only fired when the  
> e-mail address is actually updated?

Sorry hit reply before including the rest of my reply

validates\_confirmation\_of :email  
:if =\> :change\_email?

def change\_email?

# make sure that the user requested an email change

end

---

<div class="post-metadata">

**Author:** ![DuzenburyS\_Rich](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/43a26b/32.png) [@DuzenburyS\_Rich](https://www.ruby-forum.com/u/DuzenburyS_Rich)\
**Post date:** [August 16, 2006, 6:42pm UTC](https://www.ruby-forum.com/t/modeling-phony-fields/65169/4 "2006-08-16T18:42:31Z")

</div>

> You don’t need to make a database field for the email\_confirmation,  
> you can use it simply by including:
> 
> validates\_confirmation\_of :email

Yes, that’s where I started. However, when I need to update something  
completely unrelated, the email confirmation rule gets in the way:

user = User.find(1)  
user.update\_attributes(:contact\_by\_phone =\> true) =\> false  
user.errors.each\_full { |msg| puts msg } =\> ‘Email confirmation can’t be  
blank’

So, how can I structure the email confirmation rule to only fire when  
the email address is changing, either through the creation of a new  
user, or a request to actually update the email address?

Thank you.

Regards,  
Rich

---

<div class="post-metadata">

**Author:** ![DuzenburyS\_Rich](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/43a26b/32.png) [@DuzenburyS\_Rich](https://www.ruby-forum.com/u/DuzenburyS_Rich)\
**Post date:** [August 16, 2006, 7:54pm UTC](https://www.ruby-forum.com/t/modeling-phony-fields/65169/5 "2006-08-16T19:54:38Z")

</div>

> validates\_confirmation\_of :email  
> :if =\> :change\_email?
> 
> def change\_email?
> 
> # make sure that the user requested an email change
> 
> end

Hi Jim,

This is getting to the crux of the issue. I am looking for the right  
way to implement just such a ‘change\_email?’ function.

In my view, I want to display the current e-mail address, and allow the  
user to type over it if they choose. So, I currently use  
\<% form\_for :user, @user do |f| -%\>

(and so on)

When the record is retrieved from the db, of course the email field is  
filled. The confirmation is not:  
user = User.find(1)  
user.email  
“removed\_email\_address@domain.invalid”  
user.email\_confirmation  
nil

And therefore .save will not complete because user.email and  
user.email\_confirmation don’t match.

This makes perfect sense, as there is no database field for  
email\_confirmation. So, perhaps I ought to initialize  
user.email\_confirmation = user.email when a record is created or  
retrieved? I’m not sure how…but then the validation should always  
work, even if nothing gets changed.

Or, somehow, I would need to compare the email stored in the db to a  
different email field that comes back from the form. Perhaps I should  
add an email\_new accessor to the model, and then the view points to it?  
I would still need to initialize email\_new with the current value of  
email, I guess.

Anyway, this is why I wrote the list to begin with. Not sure how to  
deal with the ‘phony’ confirmation fields.

Thank you.

Regards,  
Rich

| Email \* | \<%= f.text\_field :email %\> |
| Confirm Email \* | \<%= f.text\_field :email\_confirmation %\> |

---

<div class="post-metadata">

**Author:** ![DuzenburyS\_Rich](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/43a26b/32.png) [@DuzenburyS\_Rich](https://www.ruby-forum.com/u/DuzenburyS_Rich)\
**Post date:** [August 16, 2006, 11:46pm UTC](https://www.ruby-forum.com/t/modeling-phony-fields/65169/6 "2006-08-16T23:46:06Z")

</div>

> This is getting to the crux of the issue. I am looking for the right  
> way to implement just such a ‘change\_email?’ function.
> 
> In my view, I want to display the current e-mail address, and allow  
> the
> 
> different email field that comes back from the form. Perhaps I should  
> add an email\_new accessor to the model, and then the view points to  
> it?

Well, for the archives, I did the following:

class User \< ActiveRecord::Base

validates\_confirmation\_of :email

def after\_find  
self.email\_confirmation = self.email  
end  
end

This way, any new or changing email addresses cause the  
validates\_confirmation\_of to fire, and, in cases where other things are  
getting updated, such as

user = User.find(1)  
user.update\_attributes(:contact\_by\_phone =\> false)  
user.save

The validation will operate successfully.

Thanks.

Regards,  
Rich

---

<div class="post-metadata">

**Author:** ![DuzenburyS\_Rich](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/43a26b/32.png) [@DuzenburyS\_Rich](https://www.ruby-forum.com/u/DuzenburyS_Rich)\
**Post date:** [August 16, 2006, 7:39pm UTC](https://www.ruby-forum.com/t/modeling-phony-fields/65169/7 "2006-08-16T19:39:37Z")

</div>

In theory (i.e. untested). You could store the existing email address  
in an after\_initialize handler. Then, in a before\_save (or maybe  
before\_validate?) handler, you could check if the email was different.  
If so, you could manually add errors to the object to prevent it from  
being saved.

Does that make sense?

Chris
