# Attr\_accessor\_with\_default -- how to use?

**URL:** <https://www.ruby-forum.com/t/attr-accessor-with-default-how-to-use/195185>\
**Category:** Rails\
**Created:** [September 18, 2010, 4:17pm UTC](https://www.ruby-forum.com/t/attr-accessor-with-default-how-to-use/195185 "2010-09-18T16:17:23Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![walterdavis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/a587f6/32.png) [@walterdavis](https://www.ruby-forum.com/u/walterdavis)\
**Post date:** [September 18, 2010, 4:17pm UTC](https://www.ruby-forum.com/t/attr-accessor-with-default-how-to-use/195185/1 "2010-09-18T16:17:23Z")

</div>

I have the following model:

class User \< ActiveRecord::Base

devise  
:registerable, :database\_authenticatable, :recoverable, :confirmable,  
:rememberable, :trackable, :validatable  
attr\_accessor\_with\_default :role, ‘user’

attr\_accessible :email, :password, :password\_confirmation, :first\_name,  
:last\_name, :company\_name, :phone, :role  
end

Role is a column in my database (I’m beginning to wonder if my  
troubles start there.)

Here’s a playback of an irb session on this model:

> > u = User.new  
> > =\> #\<User id: nil, email: “”, encrypted\_password: “”, password\_salt:  
> > “”, confirmation\_token: nil, confirmed\_at: nil, confirmation\_sent\_at:  
> > nil, reset\_password\_token: nil, remember\_token: nil,  
> > remember\_created\_at: nil, sign\_in\_count: 0, current\_sign\_in\_at: nil,  
> > last\_sign\_in\_at: nil, current\_sign\_in\_ip: nil, last\_sign\_in\_ip: nil,  
> > created\_at: nil, updated\_at: nil, first\_name: nil, last\_name: nil,  
> > company\_name: nil, phone: nil, role: nil\>
> 
> > u.email = ‘removed\_email\_address@domain.invalid’  
> > =\> “removed\_email\_address@domain.invalid”
> 
> > u.password = u.password\_confirmation = ‘abc123’  
> > =\> “abc123”
> 
> > u.role  
> > =\> “user”

So far, so good, I’m getting a default value here. Next, we assign a  
different value.

> > u.role = ‘admin’  
> > =\> “admin”
> 
> > u.save!  
> > =\> true

Okay, looks good. Try an immediate check to see what the value is:

> > u.role  
> > =\> “admin”

Now let’s find it again from scratch, to reload what the database  
believes is true:

> > u = User.find\_by\_email(‘removed\_email\_address@domain.invalid’)  
> > =\> #\<User id: 6, email: “removed\_email\_address@domain.invalid”, encrypted\_password:  
> > “1971f891cd2e83ef0d2fe9ba31b548cb2740573d”, password\_salt:  
> > “ENAl9O1NDurotmGjeBJT”, confirmation\_token: “SEHEmHUYtmaeaeHPLq4y”,  
> > confirmed\_at: nil, confirmation\_sent\_at: “2010-09-18 14:05:26”,  
> > reset\_password\_token: nil, remember\_token: nil, remember\_created\_at:  
> > nil, sign\_in\_count: 0, current\_sign\_in\_at: nil, last\_sign\_in\_at: nil,  
> > current\_sign\_in\_ip: nil, last\_sign\_in\_ip: nil, created\_at: “2010-09-18  
> > 14:05:26”, updated\_at: “2010-09-18 14:05:26”, first\_name: nil,  
> > last\_name: nil, company\_name: nil, phone: nil, role: nil\>

And we are at this point, where I ask WTF??

> > u.role  
> > =\> “user”

Do I have a fundamental misunderstanding of this feature? Should I be  
setting my default in the database instead, since it’s a column? What  
do you use an attr\_accessor for, let alone one with a handy default  
value?

Thanks in advance,

Walter
