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?
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.
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?