# Problem with instance variables

**URL:** <https://www.ruby-forum.com/t/problem-with-instance-variables/62431>\
**Category:** Rails\
**Created:** [June 4, 2006, 7:47pm UTC](https://www.ruby-forum.com/t/problem-with-instance-variables/62431 "2006-06-04T19:47:07Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![azazel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/838e76/32.png) [@azazel](https://www.ruby-forum.com/u/azazel)\
**Post date:** [June 4, 2006, 7:47pm UTC](https://www.ruby-forum.com/t/problem-with-instance-variables/62431/1 "2006-06-04T19:47:07Z")

</div>

Im developing a User model for my application (newbie stuff, but I have  
to start somewhere). I followed the tutorial at  
[http://sonjayatandon.com/05-2006/how-to-build-a-secured-web-application-with-ruby-on-rails/](http://sonjayatandon.com/05-2006/how-to-build-a-secured-web-application-with-ruby-on-rails/)  
and decided to add an email field. The problem is that the email is not  
being saved on the database and I end up with two email variables:  
@email and email.

validates\_presence\_of :user\_name, :password, :password\_confirmation,  
:email #:email was added by myself  
validates\_uniqueness\_of :user\_name

# email getter

def email  
@email  
end

#email setter  
def email=(eml)  
emailRE= /[\w.\_%-]+@[\w.-]+.[a-zA-Z]{2,4}/  
if eml =~ emailRE  
@email = eml  
else  
# display error message  
end  
end

Check a sample output from the console:

> > july = User.new(:user\_name =\> “july”, :password =\> “secret”, :password\_confir  
> > mation =\> “secret”)  
> > =\> #\<User:0x358e3e0 @password\_confirmation=“secret”,  
> > @attributes={“ip2”=\>nil, “a  
> > ctive\_user”=\>0, “password\_salt”=\>“W9C7g8i+”, “validateip”=\>true,  
> > “ip2\_updated”=\>  
> > nil, “password\_hash”=\>“75412fccdfc4dc313215913bf2789b67f6a23315”,  
> > “user\_name”=\>"  
> > july", “ip1\_updated”=\>nil, “ip1”=\>nil, “email”=\>“”}, @password=“secret”,  
> > @new\_re  
> > cord=true\>  
> > july.email=“removed\_email\_address@domain.invalid”  
> > =\> “removed\_email\_address@domain.invalid”  
> > july.email  
> > =\> “removed\_email\_address@domain.invalid”  
> > july  
> > =\> #\<User:0x358e3e0 @password\_confirmation=“secret”,  
> > @email=“removed\_email\_address@domain.invalid”, @a  
> > ttributes={“ip2”=\>nil, “active\_user”=\>0, “password\_salt”=\>“W9C7g8i+”,  
> > “validatei  
> > p”=\>true, “ip2\_updated”=\>nil,  
> > “password\_hash”=\>“75412fccdfc4dc313215913bf2789b67  
> > f6a23315”, “user\_name”=\>“july”, “ip1\_updated”=\>nil, “ip1”=\>nil,  
> > “email”=\>“”}, @p  
> > assword=“secret”, @new\_record=true\>

Regards,  
Roberto

---

<div class="post-metadata">

**Author:** ![azazel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/838e76/32.png) [@azazel](https://www.ruby-forum.com/u/azazel)\
**Post date:** [June 4, 2006, 8:02pm UTC](https://www.ruby-forum.com/t/problem-with-instance-variables/62431/2 "2006-06-04T20:02:06Z")

</div>

Roberto R. wrote:

> # email getter
> 
> def email  
> @email  
> end
> 
> #email setter  
> def email=(eml)  
> emailRE= /[\w.\_%-]+@[\w.-]+.[a-zA-Z]{2,4}/  
> if eml =~ emailRE  
> @email = eml  
> else  
> # display error message  
> end  
> end

Well you aren’t writing it ot the DB, you’re writing it to an instance  
variable.

You are trying way too hard here. You (almost) never need explicit  
getters and setters for database fields. Simply add the field to your  
database table, and it magically becomes available with a getter and a  
setter. No changes to your model necesary, at all. Active Record is  
that cool.

And if you want that validation, just add a second line of validation  
that looks like:

validates\_format\_of :email, :with =\> /[\w.\_%-]+@[\w.-]+.[a-zA-Z]{2,4}/
