# A real puzzler: attr\_accessor / create incompatibility?

**URL:** <https://www.ruby-forum.com/t/a-real-puzzler-attr-accessor-create-incompatibility/89684>\
**Category:** Rails\
**Created:** [March 22, 2007, 1:22am UTC](https://www.ruby-forum.com/t/a-real-puzzler-attr-accessor-create-incompatibility/89684 "2007-03-22T01:22:06Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![PSSS\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/fbc32d/32.png) [@PSSS\_S](https://www.ruby-forum.com/u/PSSS_S)\
**Post date:** [March 22, 2007, 1:22am UTC](https://www.ruby-forum.com/t/a-real-puzzler-attr-accessor-create-incompatibility/89684/1 "2007-03-22T01:22:06Z")

</div>

Greetings,

I have a problem with attr\_accessor and create seemingly being at odds  
with each other in my environment. Here is the setup:

A simple class representing a US State (say, Maine for example):

class State \< ActiveRecord::Base  
attr\_accessor(:name, :code)  
end

In an IRB session, I type the following and receive the response  
included below:

> > State.create(:name=\>‘Maine’, :code=\>‘ME’)  
> > State.create(:name=\>‘Maine’, :code=\>‘ME’)  
> > =\> #\<State:0x487be24 @new\_record=false, @attributes={“name”=\>nil,  
> > “code”=\>nil, “id”=\>2}, @code=“ME”, @name=“Maine”,  
> > @errors=#\<ActiveRecord::Errors:0x487b6cc @errors={}, @base=#\<State:  
> > 0x487be24 …\>\>\>
> 
> >

This indicates to me that somehow the persistence layer in  
ActiveRecord is not accessing the @XXX attributes but is going after  
the attributes in the @attributes hash and that these two are  
separate. As you can see, the values passed in to the create method  
are being passed through and are being assigned to the appropriate  
@XXX variables but these variables don’t seem to be used by the  
internal persistence logic.

I have tested this as follows: If I remove the attr\_accessor line from  
the State definition, it all works just fine. In fact, if I replace  
the attr\_accessor with an attr\_reader, that works fine. However, if I  
use an attr\_accessor or an attr\_writer statement, I see this rather  
unexpected behavior.

Perhaps this is expected behavior and I am missing something here? Or  
perhaps there is something I have in my environment that is causing  
this?

Any help here would be appreciated!

---

<div class="post-metadata">

**Author:** ![PSSS\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/fbc32d/32.png) [@PSSS\_S](https://www.ruby-forum.com/u/PSSS_S)\
**Post date:** [March 22, 2007, 2:55am UTC](https://www.ruby-forum.com/t/a-real-puzzler-attr-accessor-create-incompatibility/89684/2 "2007-03-22T02:55:11Z")

</div>

Hi –

On 3/21/07, P² [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> end
> 
> > >
> 
> This indicates to me that somehow the persistence layer in  
> ActiveRecord is not accessing the @XXX attributes but is going after  
> the attributes in the @attributes hash and that these two are  
> separate. As you can see, the values passed in to the create method  
> are being passed through and are being assigned to the appropriate  
> @XXX variables but these variables don’t seem to be used by the  
> internal persistence logic.

“attr\_accessor :code” creates a method called “code=” which, as you  
say, has no implications for database persistence (it just sets an  
instance variable). When you do create(:code =\> “NY”), ActiveRecord  
honors your initialization request by sending the message “code=” to  
your object. If you don’t defined “code=”, it gets handled by  
method\_missing, which knows how to map “code=” to the @attributes  
hash, where it does have database implications. But you have defined  
“code=”, so the message is recognized by the object and ActiveRecord  
never gets to do its thing.

David

–  
Q. What is THE Ruby book for Rails developers?  
A. RUBY FOR RAILS by David A. Black ([http://www.manning.com/black](http://www.manning.com/black))  
(See what readers are saying! [http://www.rubypal.com/r4rrevs.pdf](http://www.rubypal.com/r4rrevs.pdf))  
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?  
A. Ruby Power and Light, LLC ([http://www.rubypal.com](http://www.rubypal.com))

---

<div class="post-metadata">

**Author:** ![PSSS\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/fbc32d/32.png) [@PSSS\_S](https://www.ruby-forum.com/u/PSSS_S)\
**Post date:** [September 25, 2007, 11:08pm UTC](https://www.ruby-forum.com/t/a-real-puzzler-attr-accessor-create-incompatibility/89684/3 "2007-09-25T23:08:12Z")

</div>

On 3/21/07, P² [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> end
> 
> > >
> 
> the State definition, it all works just fine. In fact, if I replace  
> the attr\_accessor with an attr\_reader, that works fine. However, if I  
> use an attr\_accessor or an attr\_writer statement, I see this rather  
> unexpected behavior.
> 
> Perhaps this is expected behavior and I am missing something here? Or  
> perhaps there is something I have in my environment that is causing  
> this?
> 
> Any help here would be appreciated!

This is expected behavior. You’re basically creating your own  
attribute accessors with attr\_accessor. Check out the  
ActiveRecord::Base documentation:  
[http://rails.rubyonrails.org/classes/ActiveRecord/Base.html](http://rails.rubyonrails.org/classes/ActiveRecord/Base.html).

–  
Rick O.  
[http://lighthouseapp.com](http://lighthouseapp.com)  
[http://weblog.techno-weenie.net](http://weblog.techno-weenie.net)  
[http://mephistoblog.com](http://mephistoblog.com)
